I have the following histogram in R (for some reason, the legend does not work)
n = floor(rnorm(10000, 500, 100))
t = table(n)
barplot(t)
legend(1, 95, legend=c("group a", "group b"),
col=c("red", "black"), lty=1:2, cex=0.8)
title(main = "some title",
sub = "some title")
I want to make half of the vertical lines on this histogram "red" :
color <- c("red", "black")
color_1 <- as.factor(sample(color, 10000, replace=TRUE, prob=c(0.5, 0.5)))
t = data.frame(t)
t$color_1 <-color_1
barplot(t, color = t$color_1)
But this returns several errors:
Error in `$<-.data.frame`(`*tmp*`, color_1, value = c(1L, 1L, 1L, 2L, :
replacement has 10000 rows, data has 574
For some reason, the data has 583 rows instead of the 10,000 originally specified in (where did all the rows go!?)
n = floor(rnorm(10000, 500, 100))
Ignoring this, if I do assume 583 rows:
color <- c("red", "black")
color_1 <- as.factor(sample(color, 574, replace=TRUE, prob=c(0.5, 0.5)))
t = data.frame(t)
t$color_1 <-color_1
barplot(t, color = t$color_1)
I get a new error:
Error in barplot.default(t, color = t$color_1) :
'height' must be a vector or a matrix
Can someone please show me how to fix this error and the legend?
Thanks!