-1

I have a list of several data.frames:

my_list <- list(d1, d2, d3,...)

Each of the data.frames looks the same, resulting in a list structure like:

 my_list
   d1
      Temp
      Inc.Time
      Day
      Value
      lowlim
      uplim
   d2 (same)
   d3 (same)

I would like to plot my Data, one plot for each data.frame in the list, with the name of the df (e.g. d1) as title of the plot.

The code I would use for one dataframe at a time would be:

ForPlot <-  ggplot(d1, aes(Inc.Time, Value), fill=Inc.Time, width=.7)

ForPlot + 
  geom_hline(yintercept = c(0, 0.25, 0.5, 0.75, 1.0), colour= "lightgrey" )+
  geom_col( aes(fill=Inc.Time, group=Day), 
            position=position_dodge2(preserve="single"))+
  ylab("Relative Values") + xlab("Day") +
  ggtitle("d1")+
  facet_wrap(~Temperature,  scales= "free", ncol=3)+
  scale_y_continuous(breaks=c(0,0.25,0.5,0.75,1),labels=c(0,0.25,0.5,0.75,1),limits=c(0,1.05))+
  geom_text(aes(label = Day), position=position_dodge2(width= 0.9), 
            y=-0.025, size=2.3)+
  geom_errorbar(aes(ymin=pmax(0,lowlim), ymax=uplim), 
                position=position_dodge2())

Any ideas on how to run this on my list? Thank in advance! :)

BeccaLi
  • 174
  • 7
  • Just use `purrr::map` to apply the same plotting function to each element of your list. It's easier to show you exactly how if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jul 23 '20 at 21:52

1 Answers1

0

If you lapply through the names of a list and also pass the list into the function then you can 1. extract the data for each list item and plot, 2. add the name of the list to the plot. Here's a generic example:

library(ggplot2)

my_list <- list(a = iris, b = iris, c = iris)

p <- lapply(names(my_list), function(x, d) {
  ggplot(d[[x]], aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    geom_point() +
    labs(title = x)
}, d = my_list)

names(p) <- names(my_list)
p$a
Paul
  • 2,877
  • 1
  • 12
  • 28