[英]Several line plots by group in one PDF
我想通過測量單位繪制多年來不同資源的價格,每種資源一個圖,一個 PDF 中的所有圖。
我的完整數據集中三個資源的數據如下所示:
structure(list(year = structure(c(1994, 1996, 1996, 1997, 1997,
1998, 1998, 1998, 1998, 1999, 1999, 2000, 2001, 2001, 2002, 2002,
2002, 2003, 2003, 2003, 2004, 2004, 2004, 2004, 2005, 2005, 2005,
2005, 2005, 2006, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 2007,
2007, 2007, 2008, 2008, 2008, 2008, 2008, 2009, 2009, 2009, 2009,
2009, 2010, 2010, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2011,
2012, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013, 2014, 2014,
2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015), label = "year", format.stata = "%10.0g"),
standardmeasure = structure(c("kilograms", "million 42-gallon barrels",
"million cubic meters", "million 42-gallon barrels", "million cubic meters",
"kilograms", "metric tons", "million 42-gallon barrels",
"million cubic meters", "kilograms", "million cubic meters",
"kilograms", "kilograms", "million cubic meters", "kilograms",
"metric tons", "million cubic meters", "carats", "kilograms",
"million cubic meters", "carats", "kilograms", "metric tons",
"million cubic meters", "carats", "kilograms", "metric tons",
"million cubic meters", "thousand metric tons", "kilograms",
"metric tons", "metric tons", "million cubic meters", "thousand metric tons",
"carats", "kilograms", "metric tons", "metric tons", "million cubic meters",
"thousand metric tons", "kilograms", "metric tons", "metric tons",
"million cubic meters", "thousand metric tons", "kilograms",
"metric tons", "metric tons", "million cubic meters", "thousand metric tons",
"kilograms", "metric tons", "metric tons", "million cubic meters",
"thousand metric tons", "kilograms", "metric tons", "metric tons",
"million cubic meters", "thousand metric tons", "kilograms",
"metric tons", "metric tons", "million cubic meters", "thousand metric tons",
"kilograms", "metric tons", "metric tons", "million cubic meters",
"cubic meters", "kilograms", "metric tons", "metric tons",
"metric tons", "million cubic meters", "cubic meters", "kilograms",
"metric tons", "metric tons", "metric tons", "million cubic meters"
), label = "original unit of measurement from USGS", format.stata = "%-9s"),
wb_price_mult = structure(c(16937.037109375, NA, NA, NA,
NA, 12139.4326171875, NA, NA, NA, 11330.81640625, NA, 11089.18359375,
10529.6015625, NA, 11862.212890625, 11862213, NA, 2.72781133651733,
13639.052734375, NA, 2.9885892868042, 14942.9423828125, 14942942,
NA, 3.147536277771, 15737.6767578125, 15737677, NA, 15737677824,
20742.962890625, 20742964, NA, NA, 20742965248, 4.65880012512207,
23293.994140625, 23293994, NA, NA, 23293997056, 28583.76953125,
28583770, NA, NA, 28583772160, 31663.6328125, 31663634, NA,
NA, 31663636480, 39373.86328125, 39373864, NA, NA, 39373864960,
49430.73046875, 49430732, NA, NA, 49430736896, 51639.21484375,
51639216, NA, NA, NA, 42957.16015625, 42957160, NA, NA, NA,
37894.74609375, 37894748, NA, NA, NA, NA, 34408.5234375,
34408524, NA, NA, NA), label = "WB price in multiplier in original USGS output unit", format.stata = "%10.0g"),
resource = structure(c("gold", "natural gas", "natural gas",
"natural gas", "natural gas", "gold", "lithium", "natural gas",
"natural gas", "gold", "natural gas", "gold", "gold", "natural gas",
"gold", "gold", "natural gas", "gold", "gold", "natural gas",
"gold", "gold", "gold", "natural gas", "gold", "gold", "gold",
"natural gas", "gold", "gold", "gold", "lithium", "natural gas",
"gold", "gold", "gold", "gold", "lithium", "natural gas",
"gold", "gold", "gold", "lithium", "natural gas", "gold",
"gold", "gold", "lithium", "natural gas", "gold", "gold",
"gold", "lithium", "natural gas", "gold", "gold", "gold",
"lithium", "natural gas", "gold", "gold", "gold", "lithium",
"natural gas", "natural gas", "gold", "gold", "lithium",
"natural gas", "natural gas", "gold", "gold", "lithium",
"natural gas", "natural gas", "natural gas", "gold", "gold",
"lithium", "natural gas", "natural gas"), label = "resource", format.stata = "%-9s")), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -81L), groups = structure(list(
year = structure(c(1994, 1996, 1996, 1997, 1997, 1998, 1998,
1998, 1998, 1999, 1999, 2000, 2001, 2001, 2002, 2002, 2002,
2003, 2003, 2003, 2004, 2004, 2004, 2004, 2005, 2005, 2005,
2005, 2005, 2006, 2006, 2006, 2006, 2006, 2007, 2007, 2007,
2007, 2007, 2007, 2008, 2008, 2008, 2008, 2008, 2009, 2009,
2009, 2009, 2009, 2010, 2010, 2010, 2010, 2010, 2011, 2011,
2011, 2011, 2011, 2012, 2012, 2012, 2012, 2012, 2013, 2013,
2013, 2013, 2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015,
2015, 2015), label = "year", format.stata = "%10.0g"), standardmeasure = structure(c("kilograms",
"million 42-gallon barrels", "million cubic meters", "million 42-gallon barrels",
"million cubic meters", "kilograms", "metric tons", "million 42-gallon barrels",
"million cubic meters", "kilograms", "million cubic meters",
"kilograms", "kilograms", "million cubic meters", "kilograms",
"metric tons", "million cubic meters", "carats", "kilograms",
"million cubic meters", "carats", "kilograms", "metric tons",
"million cubic meters", "carats", "kilograms", "metric tons",
"million cubic meters", "thousand metric tons", "kilograms",
"metric tons", "metric tons", "million cubic meters", "thousand metric tons",
"carats", "kilograms", "metric tons", "metric tons", "million cubic meters",
"thousand metric tons", "kilograms", "metric tons", "metric tons",
"million cubic meters", "thousand metric tons", "kilograms",
"metric tons", "metric tons", "million cubic meters", "thousand metric tons",
"kilograms", "metric tons", "metric tons", "million cubic meters",
"thousand metric tons", "kilograms", "metric tons", "metric tons",
"million cubic meters", "thousand metric tons", "kilograms",
"metric tons", "metric tons", "million cubic meters", "thousand metric tons",
"kilograms", "metric tons", "metric tons", "million cubic meters",
"cubic meters", "kilograms", "metric tons", "metric tons",
"million cubic meters", "cubic meters", "kilograms", "metric tons",
"metric tons", "million cubic meters"), label = "original unit of measurement from USGS", format.stata = "%-9s"),
wb_price_mult = structure(c(16937.037109375, NA, NA, NA,
NA, 12139.4326171875, NA, NA, NA, 11330.81640625, NA, 11089.18359375,
10529.6015625, NA, 11862.212890625, 11862213, NA, 2.72781133651733,
13639.052734375, NA, 2.9885892868042, 14942.9423828125, 14942942,
NA, 3.147536277771, 15737.6767578125, 15737677, NA, 15737677824,
20742.962890625, 20742964, NA, NA, 20742965248, 4.65880012512207,
23293.994140625, 23293994, NA, NA, 23293997056, 28583.76953125,
28583770, NA, NA, 28583772160, 31663.6328125, 31663634, NA,
NA, 31663636480, 39373.86328125, 39373864, NA, NA, 39373864960,
49430.73046875, 49430732, NA, NA, 49430736896, 51639.21484375,
51639216, NA, NA, NA, 42957.16015625, 42957160, NA, NA, NA,
37894.74609375, 37894748, NA, NA, NA, 34408.5234375, 34408524,
NA, NA), label = "WB price in multiplier in original USGS output unit", format.stata = "%10.0g"),
.rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L,
54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73:74, 75L, 76L,
77L, 78L, 79:80, 81L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -79L), .drop = TRUE))
(我編輯了示例數據以包含所有數據的 3 個資源,而不是前 50 行,這使得數據非常不完整)
每幅圖應顯示一種資源多年來每次測量 ( standardmeasure
) 的對數價格 ( wb_price_mult
)。 最后,我想擁有所有圖表的PDF。
我用的
ggplot(example_data %>% filter(resource == "gold"), aes(x = year, y = log(wb_price_mult), color = standardmeasure)) + geom_line() + labs(title="gold")
(使用完整的數據集)
這是一種方法。
定義兩個函數,以使代碼更具可讀性,以防萬一,更易於調試。
suppressPackageStartupMessages({
library(tidyverse)
})
plot_resource <- function(X) {
title <- X$resource[1]
ggplot(X, aes(year, log(wb_price_mult), colour = standardmeasure)) +
geom_line() +
ggtitle(label = title)
}
plot_save <- function(x, filename) {
pdf(filename, onefile = TRUE)
lapply(x, print)
dev.off()
}
prices %>%
ungroup() %>%
group_split(resource) %>%
map(plot_resource) %>%
plot_save("resources.pdf")
#> Warning: Removed 11 row(s) containing missing values (geom_path).
#> Warning: Removed 27 row(s) containing missing values (geom_path).
#> png
#> 2
由reprex 包於 2022-06-18 創建 (v2.0.1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.