根据不平衡数据在 ggplot 中创建重叠直方图
Creating overlapping histograms in ggplot from unbalanced data
我在 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)
试试这个
ggplot(mapping = aes(f0)) +
geom_histogram(data = histogram, fill = "red", alpha = 0.2) +
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)
如果您最初没有指定数据,您应该能够包含具有不同长度的数据。
我在 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)
试试这个
ggplot(mapping = aes(f0)) +
geom_histogram(data = histogram, fill = "red", alpha = 0.2) +
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)
如果您最初没有指定数据,您应该能够包含具有不同长度的数据。