如何在R中使用ggplot仅对许多特定行进行着色? [英] How to color only some specific lines among many using ggplot in r?

查看:63
本文介绍了如何在R中使用ggplot仅对许多特定行进行着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gigante数据集,需要绘制它才能查看数据轨迹.我使用以下代码绘制了它的一个子集:

I have a gigante dataset that I need to plot to see the trajectory of my data. I plotted a subset of it using the code:

ggplot(Trajectory, aes(x= Passage, y =Freq, group = SNV, color= SNV)) + geom_line()

并生成情节-

这接近我的需求.现在,我想更改某些线条(所有段落中都存在的线条)的颜色

which is close to what I need. Now, I would like to change the colors of some of the lines (those that are present in all passages)

所以我尝试使用ifelse函数

so I tried to use the ifelse function

ggplot(Trajectory, aes(x= Passage, y =Freq, group = SNV, color= ifelse(Trajectory==358, "red", ifelse(Trajectory==1212, "Blue", "white")))) +
  geom_line()

它不起作用,并导致此错误消息.

It didn't work and resulted in this error message.

错误:美学必须为长度1或与数据相同(1566):x,y,组,颜色"

"Error: Aesthetics must be either length 1 or the same as the data (1566): x, y, group, colour"

任何帮助将不胜感激.

我用于绘图的数据子集可以在此处下载 https://cornell.box.com/s/vfe6144xbugdv805argpw8r5so98a73s

The subset of my data that I used for plotting can be downloaded here https://cornell.box.com/s/vfe6144xbugdv805argpw8r5so98a73s

推荐答案

感谢您的帮助.最后,我对数据集进行了子集设置,以创建一个新表,其中包含我想要更改颜色的SNV.然后,我覆盖了情节.

Thank you for your help guys. I ended up subsetting my dataset creating a new table with the SNVs that I want to color differently. Then, I overlaid the plots.

如果有帮助,这里是代码

Here is the code if it helps someone else

library(ggplot2)
ggplot(data=Trajectory) +
  geom_line(aes(x= Passage, y = Freq, group = SNV), colour ="grey57") +
  geom_line(data= P1toP5,
              aes(x= Passage, y = Freq, group = SNV), colour= "red1", linetype=2, size=1) +
  geom_line(data= P0toP5,
            aes(x= Passage, y = Freq, group = SNV), colour= "blue", size=1)

结果图是

在此处输入图片描述

-感谢一堆;)

这篇关于如何在R中使用ggplot仅对许多特定行进行着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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