[英]Produce multiples graphs in R
我的問題如下:我想用 GGplot 在 R 中生成 151 個圖。
我想開始展示我的數據:
我有一個名為“df”的第一個數據集,其中包含 152 列。 前 151 列對應於 Y 變量。 第 152 列對應於 x 變量。 雖然 151 個圖有 151 個變量,但 151 個圖的 x 變量是相同的。
此外,我還有一個名為“df_2”的數據集。 該數據集包含兩列:一列名為變量,變量名稱來自“df”,第二列具有相應的“穩定 state 值”。
基本上我想在 pdf 中有 151 個圖表,如下圖所示(帶有相關變量)
這是我獲取此類圖表的代碼
df_base = df
steady_state_df = df_2
ggplot(df_base, aes(x = df_base[, 152])) +
geom_line(aes(y = df_base$capital), col = "darkorange") +
geom_line(aes(y = steady_state_df$steady_state[steady_state_df$variables == "capital"]), col = "skyblue") +
ggtitle(paste(' Figure 11: ', "Capital", sep='')) +
theme_bw() +
theme(axis.title.x=element_blank(), axis.title.y=element_blank(), plot.title = element_text(family="Times New Roman", hjust = 0.5, margin = margin(t = 20, r = 20, b = 20, l = 20)), legend.position="none", axis.ticks.length=unit(-0.1, "cm"), axis.text.x = element_text(margin = margin(t = 10, r = 10, b = 10, l = 10)), axis.text.y = element_text(margin = margin(t = 10, r = 10, b = 10, l = 10))) +
scale_x_continuous(breaks= c(0, 20, 40, 60, 80, 100)) +
scale_x_continuous(sec.axis = dup_axis(labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(labels = NULL)) +
coord_cartesian(xlim = c(0,100), ylim = c(min(df_base$capital), max(df_base$capital)))
現在我有了這樣一個“圖形代碼模型”,我想實現一個循環來獲取這些圖形中的 151 個,並以變量名稱為索引。 這是我所做的並且不起作用:
for(i in 1:151){
path = paste("~/Desktop/plotting_task/", varnames[i], ".pdf", sep = "")
pdf(file = path, # The directory you want to save the file in
width = 5, # The width of the plot in inches
height = 5)
ggplot(df_base, aes(x = df_base[, 152])) +
geom_line(aes(y = df_base[, i]), col = "darkorange") +
geom_line(aes(y = steady_state_df$steady_state[steady_state_df$variables == varnames[i]), col = "skyblue") +
ggtitle(paste(' Figure : ', i, varnames[i], sep='')) +
theme_bw() +
theme(axis.title.x=element_blank(), axis.title.y=element_blank(), plot.title = element_text(family="Times New Roman", hjust = 0.5, margin = margin(t = 20, r = 20, b = 20, l = 20)), legend.position="none", axis.ticks.length=unit(-0.1, "cm"), axis.text.x = element_text(margin = margin(t = 10, r = 10, b = 10, l = 10)), axis.text.y = element_text(margin = margin(t = 10, r = 10, b = 10, l = 10))) +
scale_x_continuous(breaks= c(0, 20, 40, 60, 80, 100)) +
scale_x_continuous(sec.axis = dup_axis(labels = NULL)) +
scale_y_continuous(sec.axis = dup_axis(labels = NULL)) +
coord_cartesian(xlim = c(0,100), ylim = c(min(df_base[, i]), max(df_base[, i])))
dev.off()
}
有人可以幫助我嗎? 非常感謝
簡單地調用ggplot(df_base, ...) +...
不會產生繪圖圖。 是什么創建了 output 正在打印結果。
在交互式 session 中執行一行時,即執行ggplot(...
或簡單地1
,解釋器隱式調用該結果的print
。即
> 1
## ==> interpreter calls print(1)
[1] 1
> mean(1:4)
## ==> interpreter calls print(mean(1:4))
[1] 2.49
> p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
> p
## ==> interpreter calls print(p)
如果在循環內,解釋器不會調用 print。
那么ggplot
的結果是什么? 要查看內部信息,請使用str
function:
> str(p)
它不包含 plot。 相反,它包含 plot 的數據以及如何 plot 的描述。 將數據和描述轉換為實際的 plot 的魔力發生在print(p)
中,或者更確切地說,發生在ggplot2:::print.ggplot
中。
如何在循環中打印結果?
將結果分配給一個變量,然后打印它。 IE:
for (i in 1:151) {
pdf(...)
p <- ggplot(...) + ...
print(p)
dev.off()
}
編輯:如果您將調用pdf
和dev.off
置於循環之外,則所有繪圖都將寫入具有多頁的單個 pdf 文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.