Home > Mobile >  R – Replicate/Automate scatterplots for each string group in column
R – Replicate/Automate scatterplots for each string group in column

Time:01-02

I have a dataframe (170 observation and 3 columns) like so:

site_names <- c("WS1", "WS1", "WS2", "WS2", "WS3", "WS3")
x <- c(.15, .20, .17, .16, .20, .22)
y <- c(.026, .031, .045, .087, .09, .033)

df <- data.frame(site_names, x, y)

and I have a simple ggplot formula I can run for each site_name where I plot x and y accordingly:

df %>% 
filter(site_names == "") %>% 
ggplot(aes(x = x, y = y))  
geom_point()  
labs(x = "Discharge", y = "Stage")

What I would like is to automate these plots and generate 26 separate plots by site_name and save them all to a PDF

Here is what I have tried:

scatter_expl = function(x, y) {
 ggplot(df, aes(x = x, y = y) )  
      geom_point()  
      theme_bw()  
      labs(x = "Discharge", y = "Stage")
}

plots = map(df, ~scatter_expl())

plots

But this runs through the whole dataframe, returns a list and produces all scatters on the same plot: enter image description here

  •  Tags:  
  • r
  • Related