我试图制作一个像这样重叠的直方图:
ggplot(histogram, aes = (x), mapping = aes(x = value)) +
geom_histogram(data = melt(tpm_18_L_SD), breaks = seq(1,10,by = 1),
aes(y = 100*(..count../sum(..count..))), alpha=0.2) +
geom_histogram(data = melt(tpm_18_S_SD), breaks = seq(1,10,by = 1),
aes(y = 100*(..count../sum(..count..))), alpha=0.2) +
geom_histogram(data = melt(tpm_18_N_SD), breaks = seq(1,10,by = 1),
aes(y = 100*(..count../sum(..count..))), alpha=0.2) +
facet_wrap(~variable, scales = 'free_x') +
ylim(0, 20) +
ylab("Percentage of Genes") +
xlab("Standard Deviation")
我的代码只能让它们并排绘制,我也想让它们重叠。谢谢!我把它从原来的帖子里找到了,但它对我来说不起作用。它最初是3个单独的图形,我与网格和ggarrange结合。它现在看起来像这样。
以下是三个单独图表的代码。
SD_18_L <- ggplot(data = melt(tpm_18_L_SD), mapping = aes(x = value)) +
geom_histogram(aes(y = 100*(..count../sum(..count..))), breaks = seq(1, 10, by = 1)) +
facet_wrap(~variable, scales = 'free_x') +
ylim(0, 20) +
ylab("Percentage of Genes") +
xlab("Standard Deviation")
SD_18_S <- ggplot(data = melt(tpm_18_S_SD), mapping = aes(x = value)) +
geom_histogram(aes(y = 100*(..count../sum(..count..))), breaks = seq(1, 10, by = 1)) +
facet_wrap(~variable, scales = 'free_x') +
ylim(0, 20) +
ylab("Percentage of Genes") +
xlab("Standard Deviation")
SD_18_N <- ggplot(data = melt(tpm_18_N_SD), mapping = aes(x = value)) +
geom_histogram(aes(y = 100*(..count../sum(..count..))), breaks = seq(1, 10, by = 1)) +
facet_wrap(~variable, scales = 'free_x') +
ylim(0, 20) +
ylab("Percentage of Genes") +
xlab("Standard Deviation")
答案 0 :(得分:2)
ggplot
期望数据帧采用长格式。我不确定您的数据是什么样的,但您不必为每个类别调用geom_histogram
。相反,首先将所有数据放入单个数据框(您可以使用rbind
)以长格式(您已经使用melt
进行的操作),然后将其输入ggplot
并将地图填充到您的分类变量。
您对facet_wrap
的致电是将它们置于3个不同的情节中的原因。如果你想要它们都在同一个地块上,那就把它拿出来。
使用虹膜数据的示例:
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_histogram(alpha = 0.6, position = "identity")
我减少了geom_histogram
中的alpha值,因此您可以看到颜色重叠的位置,并添加了position = "identity"
,因此观察结果没有堆叠。希望有所帮助!