R: Converting "tables" to "data frame: 'height' 必须是向量或矩阵
R: Converting "tables" to "data frame: 'height' must be a vector or a matrix
我在 R 中找到了以下代码,它绘制了一个直方图并将一些条形着色为红色:
n = floor(rnorm(10000, 500, 100))
t = table(n)
# get color counts
r <- floor(length(t)/2)
b <- length(t) - r
# repeat red r times, repeat black b times
barplot(t, col = rep(c("red", "black"), c(r, b)), border = NA)
# use "topleft" to place legend
legend("topleft", legend=c("group a", "group b"),
col=c("red", "black"), lty = 1, cex=0.8)
title(main = "some title",
sub = "some title")
如上述代码所示,这要求数据为“table”格式。
问题: 我正在尝试制作相同的图表,但使用“数据框”代替。这将使我更加灵活(例如,将颜色随机分配给直方图中的条形),因为我对数据框更加熟悉。我尝试编写以下代码:
#randomly assign colors to different bars in the histogram and convert table into data frame
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)
但是这个returns几个错误:
Error in `$<-.data.frame`(`*tmp*`, color_1, value = c(1L, 1L, 1L, 2L, :
replacement has 10000 rows, data has 574
出于某种原因,数据有 583 行,而不是最初在(所有行去哪儿了!?)n = floor(rnorm(10000, 500, 100))
命令中指定的 10,000 行。
忽略这个,如果我假设有 583 行:
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)
我收到一个新错误:
Error in barplot.default(t, color = t$color_1) :
'height' must be a vector or a matrix
有人可以告诉我如何解决这个错误吗?
谢谢
使用公式方法 data.frame
t <- as.data.frame(t)
color <- c("red", "black")
color_1 <- sample(color, nrow(t), replace=TRUE, prob=c(0.5, 0.5))
barplot(Freq ~ n, data = t, col = color_1, border = NA)
-输出
我在 R 中找到了以下代码,它绘制了一个直方图并将一些条形着色为红色:
n = floor(rnorm(10000, 500, 100))
t = table(n)
# get color counts
r <- floor(length(t)/2)
b <- length(t) - r
# repeat red r times, repeat black b times
barplot(t, col = rep(c("red", "black"), c(r, b)), border = NA)
# use "topleft" to place legend
legend("topleft", legend=c("group a", "group b"),
col=c("red", "black"), lty = 1, cex=0.8)
title(main = "some title",
sub = "some title")
如上述代码所示,这要求数据为“table”格式。
问题: 我正在尝试制作相同的图表,但使用“数据框”代替。这将使我更加灵活(例如,将颜色随机分配给直方图中的条形),因为我对数据框更加熟悉。我尝试编写以下代码:
#randomly assign colors to different bars in the histogram and convert table into data frame
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)
但是这个returns几个错误:
Error in `$<-.data.frame`(`*tmp*`, color_1, value = c(1L, 1L, 1L, 2L, :
replacement has 10000 rows, data has 574
出于某种原因,数据有 583 行,而不是最初在(所有行去哪儿了!?)n = floor(rnorm(10000, 500, 100))
命令中指定的 10,000 行。
忽略这个,如果我假设有 583 行:
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)
我收到一个新错误:
Error in barplot.default(t, color = t$color_1) :
'height' must be a vector or a matrix
有人可以告诉我如何解决这个错误吗?
谢谢
使用公式方法 data.frame
t <- as.data.frame(t)
color <- c("red", "black")
color_1 <- sample(color, nrow(t), replace=TRUE, prob=c(0.5, 0.5))
barplot(Freq ~ n, data = t, col = color_1, border = NA)
-输出