子集和ggplot2 [英] Subset and ggplot2

查看:145
本文介绍了子集和ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,要用ggplot2绘制一个数据框的子集。我的DF如下:

  ID值1价值2 
P1 100 12
P1 120 13
...
P2 300 11
P2 400 16
...
P3 130 15
P3 140 12
...

现在我怎样才能为值P1和P3绘制Value1与Value2?
例如我试过:

pre $ g $ p $ ggplot(subset(df,ID ==P1& P3)+ geom_line(aes(Value1,Value2,group = ID,color = ID)))

收到错误。



ps我也尝试过许多与P1& P3,但我总是失败。

解决方案

这里有2个子集选项:

使用子集从基本R开始:

  library(ggplot2) 
geg_line(aes(Value1,Value2,group = ID,color = ID))
ggplot(子集(dat,ID%in%c(P1,P3)))+
$ b

使用 subset geom_line (注意我使用 plyr 包来使用特殊的函数)。

  library(plyr)
ggplot(data = dat)+
geom_line(aes(Value1,Value2 ,group = ID,color = ID),
,subset =。(ID%in%c(P1,P3)))

您也可以使用补充子集:

$ p $子集(dat, ID!=P2)


I have a problem to plot a subset of a data frame with ggplot2. My df is like:

ID Value1 Value2
P1 100 12
P1 120 13
...
P2 300 11
P2 400 16
...
P3 130 15
P3 140 12
...

How can I now plot Value1 vs Value2 only for IDs P1 and P3? For example I tried:

ggplot(subset(df,ID=="P1 & P3") + geom_line(aes(Value1, Value2, group=ID, colour=ID)))

but I always receive an error.

p.s. I also tried many combination with P1 & P3 but I always failed..

解决方案

Here 2 options for subsetting:

Using subset from base R:

library(ggplot2)
ggplot(subset(dat,ID %in% c("P1" , "P3"))) + 
         geom_line(aes(Value1, Value2, group=ID, colour=ID))

Using subset the argument of geom_line(Note I am using plyr package to use the special . function).

library(plyr)
ggplot(data=dat)+ 
  geom_line(aes(Value1, Value2, group=ID, colour=ID),
                ,subset = .(ID %in% c("P1" , "P3")))

You can also use the complementary subsetting:

subset(dat,ID != "P2")

这篇关于子集和ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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