我想在R中把我的數字匯出為png或pdf或jpeg檔案。 簡單地說,我想:
- 在用戶定義的函式
graphplotter中包含匯出代碼為png/pdf/jpeg(請參考下面的代碼)。
# dataset。
Gen < - c('M'/span>。 'M','M'。 'M','F'。 'F','F','F'。 'M','M'。 'M','M'。 'F','F'。 'F','F')
網站 < - rep('FRX'。 length(Gen))
輸入< - 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)
時間_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)
時間_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)
時間_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)
時間_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)
時間_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
# 下面的graphplotter函式生成了一個圖形,我想要的是將輸出匯出為一個png檔案。
library(tidyr)
library(ggplot2)
graphplotter <-function(x){>
x%>%
gather(. ,時間,濁度,Time_1。 Time_5, factor_key=TRUE) %>;%
mutate(label = (paste0(Gen。 "-"。 型別)) %> %
#group_by(UID) %>%
ggplot(aes(color = label)) geom_point(aes(time, 濁度,形狀= 標簽。 組= label))
geom_line(aes(時間,濁度。 組= label)) facet_wrap(~UID) 主題()
圖例。 position = c(1。 1),
圖例。 justification = c("right"。 "top"),
legend.box.just = "right",。
圖例。 margin = margin(1,/span> 1, 1, 1),
圖例。 text = element_text(size = 7))
#1.匯出為png檔案的代碼。
} 2.
graphplotter(df)
uj5u.com熱心網友回復:
你可以像這樣使用ggplot2::ggsave()
graphplotter < -function(x, plot_name, file_type){
plot <- x %>%
收集(. ,時間,濁度,Time_1。 Time_5, factor_key=TRUE) %>%
mutate(label = (paste0(Gen。 "-"。 型別)) %> %
#group_by(UID) %>%
ggplot(aes(color = label)) geom_point(aes(time, 濁度,形狀= 標簽。 組= label))
geom_line(aes(時間,濁度。 組= label)) facet_wrap(~UID) 主題()
圖例。 position = c(1。 1),
圖例。 justification = c("right"。 "top"),
legend.box.just = "right",。
圖例。 margin = margin(1,/span> 1, 1, 1),
圖例。 text = element_text(size = 7))
#1.匯出為png檔案的代碼。
ggsave()
檔案名= plot_name,
plot = plot,
設備 = file_type
)
}。
設備引數將讓你指定檔案型別(無論是.png,還是.jpeg,等等)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/317213.html
標籤:

