R:如何将绘图/图形导出为 png 或 pdf 文件
Posted
技术标签:
【中文标题】R:如何将绘图/图形导出为 png 或 pdf 文件【英文标题】:R: How to export plots/figures as a png or pdf file 【发布时间】:2021-11-10 00:00:15 【问题描述】:我想将我的图形导出为 R 中的 png
或 pdf
或 jpeg
文件。简单来说,我想:
-
在用户自定义函数
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
@camille,我在哪里包含它?您能否通过包含 ggplot2::ggsave
共享完整代码来做出回应。谢谢
【参考方案1】:
你可以像这样使用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
等)
【讨论】:
谢谢,在定义graphplotter
函数后,graphplotter( arg1, arg2, arg3)
中的内容是什么?
arg1 是数据,2 是绘图名称(例如“exmaple.plot”),3 是文件类型(例如“png”),如下所示:graphplotter(x = df, plot_name = "example-plot", file_type = "png")
感谢您的澄清。但是,该文件保存为XMLSpy
文件而不是png
。请参考我的代码截图(上图)。我不知道我做错了什么。
您是否尝试将绘图名称更改为您想要的文件类型?例如graphplotter(x = df, plot_name = "example-plot.png", file_type = "png")
?我可以重现你的代码来测试它。以上是关于R:如何将绘图/图形导出为 png 或 pdf 文件的主要内容,如果未能解决你的问题,请参考以下文章
将 RStudio 中的绘图另存为 PNG 或将 PDF 转换为 PNG 后缀