简体   繁体   中英

R: How to plot data of data.frames in a list?

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 (eg 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: :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM