从列表中提取单个图

时间:2018-08-23 19:03:48

标签: r ggplot2

我正在通过构面和列表制作多个图,然后使用ggsave将它们另存为pdf。我试图从构面中提取单个图以自行保存,但无法从清单中弄清楚该如何做。

这是我的代码:

library(ggplot2)
library(gridExtra)
library(cowplot)
ChlFPALL <- read.csv("H:/Feeding Experiments/2018/FeedingFluoroprobeALL.csv")

ChlFPALL$Variable <- factor(ChlFPALL$Variable, levels=c("Green Algae",  "Bluegreen",    "Diatoms",  "Cryptophyta",  "Yellow substances",    "Total conc.", "Green Algae FM",    "Bluegreen FM", "Diatoms FM",   "Cryptophyta FM",   "Yellow substances FM", "Total conc. FM"), labels=c("Green Algae",  "Bluegreen",    "Diatoms",  "Cryptophyta",  "Yellow substances",    "Total conc.", "Green Algae FM",    "Bluegreen FM", "Diatoms FM",   "Cryptophyta FM",   "Yellow substances FM", "Total conc. FM"))

#Set y axis title#
ytitle <- expression(bold(paste("Chlorophyll", italic(" a "), mu, gL^-1)))

ChlFPALL$Tank <- factor(ChlFPALL$Tank,levels=c("C1","C2","C3","Q1","Q2","Q3","Q4","Average"))


#Set Groups#
Subset<-subset(ChlFPALL, IDCode=="WE4192")

pd <- position_dodge(0.1) # move them .05 to the left and right


plist = lapply(split(Subset, Subset$Tank), function(d) {
  ggplot(data=d, aes(x=TimePoint, y=Chl, group=Variable, colour=Color, shape=Shape)) +
    geom_errorbar(aes(ymin=Chl-SE, ymax=Chl+SE), width=.1) +
    geom_line(size=1) +
    geom_point(size=4) + scale_x_continuous(breaks = seq(0, 3, 1)) + 
    geom_point() +
    facet_wrap(~ Tank) +
    scale_y_continuous(limits=c(0, max(Subset$Chl, na.rm=TRUE))) + 
    labs(x = "Time Point (Hours)", y = ytitle, 
          colour = NULL, shape = NULL) +

    theme(plot.margin=unit(rep(0.4,4),"lines")) +
          theme(plot.subtitle = element_text(vjust = 1), 
                                              plot.caption = element_text(vjust = 1), 
                                               axis.text = element_text(size = 10, 
                                                                                                                 face = "bold", colour = "black"), 
                                              legend.text = element_text(size = 10, 
                                                                         face = "bold"), legend.key = element_rect(fill = NA), 
                                              legend.background = element_rect(fill = NA)) + scale_colour_manual(values = c("Bluegreen" = "#528B8B", "Cryptophyta" = "#8B4513", "Diatoms"="#A52A2A", "Green Algae" = "#008B00", "Total conc." = "#000000", "Yellow substances"= "#EEEE00")) 
})



plots <- marrangeGrob(plist, nrow = 2, ncol = 2)

ggsave("multipage2.pdf", plots, width = 11, height = 8.5, units = "in")

这为我提供了C1,C2,C3 ...的图。例如,如何从列表中仅获取C2?

就此标记为重复的内容而言,我无法通过搜索找到它。但是还是谢谢@alistaire

1 个答案:

答案 0 :(得分:0)

plist [[1]]输出第一个图形,plist [[2]]输出第二个图形,