-3

I want to filter my data according to sites, how can I do that for the following code? I am trying to use package dplyr, but I am unable to filter.

myData2 <- ggplot(myData,aes(year,bleaching)) +
  geom_point(aes(color = site))+ facet_wrap(~kind) 
myData2
myData3 <- myData2 + geom_smooth(aes(group = 1),
               method = "lm",
               color = "black",
               formula = y~ poly(x, 2),
               se = FALSE)
myData3
library(dplyr)
filter(myData3,site == "site02")
myData3
Jaap
  • 81,064
  • 34
  • 182
  • 193
DataLover
  • 1
  • 1
  • You are trying to filter a `ggplot` object, not a `data.frame`. Filter `myData` and then plot it. – bVa Apr 03 '17 at 11:18
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Apr 03 '17 at 11:27

1 Answers1

0

I think you're doing some things that are bad practice in your example code - namely creating ggplot objects and naming them 'mydata' (it's a plot, not a dataframe).

So let's clean up a bit.

First, we declare the data frame we want to plot

mydata_site02 <- filter(myData,site == "site02")

Then, we make our plot.

ggplot(myData,aes(year,bleaching)) +
geom_point(aes(color = site))+ 
facet_wrap(~kind) + 
geom_smooth(aes(group = 1),
method = "lm",
color = "black",
formula = y~ poly(x, 2),
           se = FALSE)
WHoekstra
  • 173
  • 7