ggplot not adding legend. What am I missing? very new to R

你。 提交于 2019-12-11 18:14:05

问题


I'm plotting three samples with ggplot but it's not adding a legend for the samples. It's not spitting out any error message so I'm not sure where I'm going wrong. I'd really appreicate some guidance.

I've tried to declare color for each sample for the legend manually but there is still no legend on the plot.

df<-data.frame(samples$V1, samples$V2, samples$V3, samples$V4, samples$V5, samples$V6, samples$V7)


CG_methplot <- ggplot(df, aes(x=samples$V1,))+
  scale_x_continuous(breaks=number_ticks(10))+
  xlab("bins")+
  ylab("mean CG methylation")+
  geom_point(aes(y=samples$V2), size=3, colour='#009933')+
  geom_point(aes(y=samples$V3), size=3, colour='#FF0000')+
  geom_point(aes(y=samples$V4), size=3, colour='#0033FF')+
  scale_color_manual(values=c("samples1"="009933", "sample2"="FF0000", "sample3" ="0033FF"))
CG_methplot

As requested, sample data.

head(df)

samples.V1 samples.V2 samples.V3 samples.V4 samples.V5 samples.V6 samples.V7
1          1   0.033636   0.027857   0.028830   0.029836   0.024457   0.024930
2          2   0.032094   0.029620   0.028005   0.028294   0.026220   0.024105
3          3   0.032011   0.027212   0.029728   0.028211   0.023812   0.025828
4          4   0.030857   0.029833   0.028907   0.027057   0.026433   0.025007
5          5   0.028480   0.028080   0.028553   0.024680   0.024680   0.024653
6          6   0.029445   0.027099   0.029346   0.025645   0.023699   0.025446

回答1:


library(reshape2)
melted <- melt(df, id.vars = "V1")

p <- ggplot(melted, aes(x = V1, y = value, colour = variable))
p + geom_point()



来源:https://stackoverflow.com/questions/21616688/ggplot-not-adding-legend-what-am-i-missing-very-new-to-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!