我在R中使用ggplot2绘制多个重叠的直方图。我在其他答案中看到这样的:
ggplot(histogram, aes(f0)) +
geom_histogram(data = lowf0, fill = "red", alpha = 0.2) +
geom_histogram(data = mediumf0, fill = "blue", alpha = 0.2) +
geom_histogram(data = highf0, fill = "green", alpha = 0.2)
如果我的数据来自相等长度的向量,但不是相同长度的向量,则此方法将非常有效。我目前已进行了此设置,以使用两个不同的单矢量数据框分别创建两个单独的图。我四处张望,但无法找到具有不同大小数据的任何东西
您可以对数据进行结构化,以便有一个包含级别的列。然后ggplot
有一个分组选项
times=c(10, 20, 30)
dat <- data.frame(levs=rep(c("low", "med", "high"), times=times),
counts=rexp(60, rate=c(rep(c(0.1, 0.2, 0.3), times=times))))
ggplot(dat, aes(counts, fill=levs, group=levs)) +
geom_histogram(position="dodge", alpha=0.2)
重叠密度可能看起来更好
ggplot(dat, aes(counts, fill=levs, group=levs)) +
geom_density(alpha=0.2)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句