【发布时间】:2021-11-29 09:12:08
【问题描述】:
我尝试在 R 中制作以下直方图(随机选择所有行的 10% 并将它们涂成红色):
a = rnorm(100000,60000,1000)
b = a
c = data.frame(a,b)
color <- c("black", "red")
color_1 <- sample(color, nrow(c), replace=TRUE, prob=c(0.9, 0.1))
c$color_1 = as.factor(color_1)
hist(c$a, col = c$color_1, 100000, main = "title")
legend("topleft", legend=c("group a", "group b"),
col=c("red", "black"), lty = 1, cex=0.8)
title(
sub = "some title")
问题:但由于某种原因,颜色没有显示出来:
我尝试查看其他命令是否可以显示颜色:
hist(c$a, col = color_1, 100000, main = "title")
或尝试将颜色变量作为“因素”移除:
a = rnorm(100000,60000,1000)
b = a
c = data.frame(a,b)
color <- c("black", "red")
color_1 <- sample(color, nrow(c), replace=TRUE, prob=c(0.9, 0.1))
c$color_1 = color_1
hist(c$a, col = c$color_1, 100000, main = "title")
legend("topleft", legend=c("group a", "group b"),
col=c("red", "black"), lty = 1, cex=0.8)
title(
sub = "some title")
我也尝试在此处 (Partially color histogram in R) 遵循此问题的建议:
h = hist(c$a, col = c$color_1, breaks = 100000, main = "title")
legend("topleft", legend=c("group a", "group b"),
col=c("red", "black"), lty = 1, cex=0.8)
title(
sub = "some title")
cuts <- cut(h$breaks, c(-Inf,Inf))
plot(h, col=cuts)
但这也不起作用。我想这可能是因为我没有正确使用“cut”功能?
谁能告诉我如何解决这个问题?
谢谢
【问题讨论】:
-
如果您随机选择直方图条,您没有 1) 切点; 2)
b是什么意思?其余代码中未使用它; 3) 你真的想要 100K 条形图对应 100K 数据点吗?
标签: r plot graph data-visualization data-manipulation