ggplot没有添加图例。我错过了什么? R很新

时间:2014-02-06 23:59:20

标签: r ggplot2 format scale legend

我正在使用ggplot绘制三个样本,但它没有为样本添加图例。它没有吐出任何错误消息所以我不确定我哪里出错了。我真的会提供一些指导。

我试图手动为图例声明每个样本的颜色,但图中仍然没有图例。

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

根据要求提供样本数据。

  

头(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 个答案:

答案 0 :(得分:4)

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

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

enter image description here