R: 如何将 plots/figures 导出为 png 或 pdf 文件

R: How to export plots/figures as a png or pdf file

我想将我的图形导出为 R 中的 pngpdfjpeg 文件。简单来说,我想:

  1. 在用户自定义函数graphplotter中包含导出代码png/pdf/jpeg(请参考下面的代码)。
# dataset
Gen <- c('M','M','M','M','F','F','F','F','M','M','M','M','F','F','F','F')
Site <- rep('FRX',length(Gen))
Type <- c('L','L','L','L','L','L','L','L','R','R','R','R','R','R','R','R')
UID <- c(1001,1002,1003,1004,1001,1002,1003,1004,1001,1002,1003,1004,1001,1002,1003,1004)
Time_1 <- c(100.78,112.34,108.52,139.19,149.02,177.77,79.18,89.10,106.78,102.34,128.52,119.19,129.02,147.77,169.18,170.11)
Time_2 <- c(150.78,162.34,188.53,197.69,208.07,217.76,229.48,139.51,146.87,182.54,189.57,199.97,229.28,247.73,269.91,249.19)
Time_3 <- c(250.78,262.34,288.53,297.69,308.07,317.7,329.81,339.15,346.87,382.54,369.59,399.97,329.28,347.73,369.91,349.19)
Time_4 <- c(240.18,232.14,258.53,276.69,338.07,307.74,359.16,339.25,365.87,392.48,399.97,410.75,429.08,448.39,465.15,469.33)
Time_5 <-c(270.84,282.14,298.53,306.69,318.73,327.47,369.63,389.59,398.75,432.18,449.78,473.55,494.85,509.39,515.52,539.23)
df <- data.frame(Gen,Site,Type,UID,Time_1,Time_2,Time_3,Time_4,Time_5)
df

######################################################################################
# I know exporting as png looks something like this: 
png(filename="turbidity-vs-time.png") 
dev.off()

#####################################################################################
# I will like to include the 'exporting as png file code' in the "graphplotter" function below

##################################################################################
# user defined code to plot multiple graphs 
# the graphplotter function below generates a graph, all I want is to export the output as a png file

library(tidyr)
library(ggplot2)
graphplotter <-function(x){
  x %>%
    gather(., time, turbidity, Time_1:Time_5, factor_key=TRUE) %>%
    mutate(label = (paste0(Gen, "-", Type))) %>%
    #group_by(UID) %>%
    ggplot(aes(color = label)) + geom_point(aes(time, turbidity, shape = label, group = label)) +
    geom_line(aes(time, turbidity, group = label)) + facet_wrap(~UID) + theme(
      legend.position = c(1, 1),
      legend.justification = c("right", "top"),
      legend.box.just = "right",
      legend.margin = margin(1, 1, 1, 1),
      legend.text = element_text(size = 7))
      
      #1.  export as png file code


}

graphplotter(df)



更新: @大卫 提前致谢!

你可以像这样使用ggplot2::ggsave()

graphplotter <-function(x, plot_name, file_type){
  plot <- x %>%
    gather(., time, turbidity, Time_1:Time_5, factor_key=TRUE) %>%
    mutate(label = (paste0(Gen, "-", Type))) %>%
    #group_by(UID) %>%
    ggplot(aes(color = label)) + geom_point(aes(time, turbidity, shape = label, group = label)) +
    geom_line(aes(time, turbidity, group = label)) + facet_wrap(~UID) + theme(
      legend.position = c(1, 1),
      legend.justification = c("right", "top"),
      legend.box.just = "right",
      legend.margin = margin(1, 1, 1, 1),
      legend.text = element_text(size = 7))
  
  #1.  export as png file code
  
  ggsave(
    filename = plot_name,
    plot = plot,
    device = file_type
  )
}

设备参数将让您指定文件类型(.png.jpeg 等)