Subset and ggplot2

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.

ps 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")

Are you looking for the following plot:

library(ggplot2) 
l<-df[df$ID %in% c("P1","P3"),]
myplot<-ggplot(l)+geom_line(aes(Value1, Value2, group=ID, colour=ID))

在这里输入图像描述


还有另一种解决方案,我觉得很有用,特别是当我想绘制同一对象的多个子集时:

myplot<-ggplot(df)+geom_line(aes(Value1, Value2, group=ID, colour=ID))
myplot %+% subset(df, ID %in% c("P1","P3"))
myplot %+% subset(df, ID %in% c("P2"))
链接地址: http://www.djcxy.com/p/86156.html

上一篇: ggplot2次轴奇怪的输出

下一篇: 子集和ggplot2