ggplot不添加图例。我错过了什么? R很新 [英] ggplot not adding legend. What am I missing? very new to R

查看:389
本文介绍了ggplot不添加图例。我错过了什么? R很新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ggplot绘制三个样本,但没有为样本添加图例。这不是吐出任何错误信息,所以我不知道我要去哪里错了。我真的想知道一些指导。

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.


头(df)

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


推荐答案

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

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

这篇关于ggplot不添加图例。我错过了什么? R很新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆