【发布时间】:2014-07-24 11:53:04
【问题描述】:
我有这个代码
ggplot()
+ geom_histogram(aes(x=V1, y=(..count..)/sum(..count..)), fill="red", alpha=.4, colour="red", data=coding, stat = "bin", binwidth = 30)
+ geom_histogram(aes(x=V1,y=(..count..)/sum(..count..)), fill="blue", alpha=.4, colour="blue", data=lncrna, stat = "bin", binwidth = 30)
+ coord_cartesian(xlim = c(0, 2000))
+ xlab("Size (nt)")
+ ylab("Percentage (%)")
+ geom_vline(data=cdf, aes(xintercept=rating.mean, colour=Labels), linetype="dashed", size=1)
生成没有图例的漂亮直方图:
在我遇到相同问题的每个帖子中,他们都说将color 放入aes。然而,这并没有给出任何传说。
我试过了:
ggplot() + geom_histogram(aes(x=V1, y=(..count..)/sum(..count..),color="red", fill="red"), fill="red", alpha=.4, colour="red", data=coding, stat = "bin", binwidth = 30)
+ geom_histogram(aes(x=V1,y=(..count..)/sum(..count..), color="blue", fill="blue"), fill="blue", alpha=.4, colour="blue", data=lncrna, stat = "bin", binwidth = 30)
+ coord_cartesian(xlim = c(0, 2000))
+ xlab("Size (nt)")
+ ylab("Percentage (%)")
+ geom_vline(data=cdf, aes(xintercept=rating.mean, colour=Labels), linetype="dashed", size=1)
没有成功。
如何在图表中添加图例?
【问题讨论】:
-
您是否尝试在美学映射中指定填充/颜色?我在你的代码中没有看到。一些example data 可以更轻松地向您展示如何做到这一点。
-
什么是美学映射?
-
@user2979409:这是你使用的
aes()函数。 -
如果在 aes 内指定,则不应在 aes 外指定。
-
我会将
coding和lncrna合并到一个对象中,并附加一列来说明数据来自(假设变量名为origin)。您将其映射到aes(),例如ggplot(merged.data, aes(..., fill = origin)) + geom_histogram()。您可以通过比例控制特定的颜色。见docs.ggplot2.org/current