0

here's my data:

head(df)

  FY      Analyte Value
  <fct>   <fct>   <dbl>
1 2007-08 CONF(G) 634  
2 2007-08 PH(G)     7.8
3 2007-08 TEMP(G)  24.8
4 2007-08 UHS(G)    2.5
5 2007-08 FC(G)     0.5
6 2007-08 CBOD(C)   1   

My dataset is a long df, spanning 10 years. I want to create multiple ggplots (of each Analyte) where the x axis is FY (financial year) and the y axis is Value. Ideally the Y axis title would also change based on the variable being plotted.

I've seen a few reproducible chunks of code in my search to do this but none of them seem to apply to a long dataframe (where I want to loop through each level of the Analyte variable). I also want it to save to my working directory (possibly using the png and dev.off() functions).

Anyone know a solution? Thanks!

1 Answer 1

1

Split the data for each Analyte and use map to save the plot as separate image.

library(tidyverse)

df %>%
  group_split(Analyte) %>%
  map(~{
    analyte_name <- .$Analyte[1]
    tmp <- ggplot(., aes(FY, Value)) + geom_boxplot() + ggtitle(analyte_name)
    ggsave(paste0(analyte_name, '.png'), tmp)
  })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.