【发布时间】:2021-02-01 01:40:47
【问题描述】:
我用这种性质的数据创建了一个堆叠直方图:
> haps
tot K7a
1 1 2
2 7 1
3 1 4
4 8 3
5 1 6
tot 是每个元素的观察计数,(我在上表中仅显示 1899 中的 5 个元素), K7a 是一个有 7 个级别的因子。 应读取数据,例如如:元素1被观察一次属于第2组,...元素4被观察8次属于第3组,以此类推
使用以下代码,我得到一个黑白直方图:
ggplot(haps, aes(x=tot) +
geom_histogram(binwidth = 0.5) +
scale_y_continuous(trans="log1p", breaks = c(1, 2, 3, 4, 5, 10, 15, 20, 25, 50, 100, 500, 1000, 1500, 2000)) +
scale_x_continuous(limits = c(0, 20), breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+
theme_gray(base_size = 10) + labs(x = "Haplotype repeats", y = "Number of events" )
然后我想给7组加颜色,我用下面的代码,简单的给aes加填充:
ggplot(haps, aes(x=tot, fill=K7a)) +
geom_histogram(binwidth = 0.5) +
scale_y_continuous(trans="log1p", breaks = c(1, 2, 3, 4, 5, 10, 15, 20, 25, 50, 100, 500, 1000, 1500, 2000)) +
(limits = c(0, 20), breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20))+
theme_gray(base_size = 10) + labs(x = "Haplotype repeats", y = "Number of events" )
即使我使用相同的代码,第二张图也会改变 y 比例,但我不知道如何获得与黑白图相同比例的图。
【问题讨论】:
标签: r ggplot2 histogram stacked