我正在尝试制作一个重叠的直方图,如下所示:

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个独立的图,我将它们与grid和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")我的图表现在是什么样子:

发布于 2018-02-23 23:45:59
ggplot期望数据帧采用长格式。我不确定您的数据是什么样子,但您不应该为每个类别调用geom_histogram。取而代之的是,首先将所有数据放入一个长格式的数据帧(您可以为此使用rbind )(您已经在使用melt),然后将其输入到ggplot中,并将fill映射到您的分类变量。
您对facet_wrap的调用将它们放在3个不同的图中。如果你想把它们都放在同一张图上,去掉那条线。
使用虹膜数据的示例:
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_histogram(alpha = 0.6, position = "identity")我减少了geom_histogram中的alpha,这样你就可以看到颜色重叠的地方,并添加了position = "identity",这样观察就不会被堆叠。希望这能有所帮助!
https://stackoverflow.com/questions/48940077
复制相似问题