matteo matteo - 2 months ago 40
R Question

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.

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


Here 2 options for subsetting:

Using subset from base R:

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

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