Subset and ggplot2

2020-01-24 11:40发布

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..

标签: r ggplot2 subset
8条回答
甜甜的少女心
2楼-- · 2020-01-24 12:05

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

enter image description here

查看更多
太酷不给撩
3楼-- · 2020-01-24 12:08

@agstudy's answer didn't work for me with the latest version of ggplot2, but this did, using maggritr pipes:

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

It works because if geom_line sees that data is a function, it will call that function with the inherited version of data and use the output of that function as data.

查看更多
相关推荐>>
4楼-- · 2020-01-24 12:12

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")
查看更多
Explosion°爆炸
5楼-- · 2020-01-24 12:16

Try filter to subset only the rows of P1 and P3

df2 <- filter(df, ID == "P1" | ID == "P3")

Than yo can plot Value1. vs Value2.

查看更多
一夜七次
6楼-- · 2020-01-24 12:20

There's another solution that I find useful, especially when I want to plot multiple subsets of the same object:

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"))
查看更多
Bombasti
7楼-- · 2020-01-24 12:23

Your formulation is almost correct. You want:

subset(dat, ID=="P1" | ID=="P3") 

Where the | ('pipe') means 'or'. Your solution, ID=="P1 & P3", is looking for a case where ID is literally "P1 & P3"

查看更多
登录 后发表回答