Martin Jeret Martin Jeret - 3 years ago 189
R Question

how to use geom_smooth in ggplot 2 for a subset of values in a data frame

I have a data frame with 3 keys Names, Week, Hours. There are 6 names and I would like to geom_smooth() the scattered plot, but do not want to include values associated with 1 name in the smoothing. It is a graph to see how the hours of 1 person affect the hours of the team.

my data looks something like this
Week<date> Names<chr> Hours<dbl>
2017-02-27 Miki 4.50
2017-02-27 Mouse 1.20
2017-02-27 Rocks 3.70
2017-03-06 Miki 5.80
2017-03-06 Mouse 2.20
2017-03-06 Rocks 11.00


ggplot(a, aes(x=Week, y=Hours))+
geom_smooth(aes(group_by(subset(Hours, !("Rocks" %in% Names)))))+
geom_smooth(aes(group_by(subset(Hours, ("Rocks" %in% Names)))))+
scale_x_date(date_labels = "%Y/%m/%d", date_breaks="1 month", date_minor_breaks = "1 week")

I get this error code:
Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "c('double', 'numeric')"

Answer Source

You should modify your dataframe to exclude/filter 'Rocks' for the 2nd and 3rd layer.

a %>% ggplot(aes(x = Week, y = Hours)) + 
 geom_point(aes(color = Names)) +
 geom_smooth(data = a %>% filter(!Names %in% 'Rocks')) + 
 geom_smooth(data = a %>% filter(Names %in% 'Rocks'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download