简体   繁体   English

在一个 PDF 中按组绘制多个线图

[英]Several line plots by group in one PDF

I would like to plot prices for different resources over the years by their measurement unit with one plot for each resource and all plots in one PDF.我想通过测量单位绘制多年来不同资源的价格,每种资源一个图,一个 PDF 中的所有图。

My data for three of the resources in my full dataset looks like:我的完整数据集中三个资源的数据如下所示:

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))

(I edited the example data to include 3 resources with all data instead of the first 50 rows, which makes for very incomplete data) (我编辑了示例数据以包含所有数据的 3 个资源,而不是前 50 行,这使得数据非常不完整)

Each plot should show the log-prices ( wb_price_mult ) for one resource over the years for each measurement ( standardmeasure ).每幅图应显示一种资源多年来每次测量 ( standardmeasure ) 的对数价格 ( wb_price_mult )。 At the end I would like to have on PDF with all graphs.最后,我想拥有所有图表的PDF。

Each graph should look something like:每个图表应类似于: 资源黄金的完整数据集的示例图

which I made with我用的

ggplot(example_data %>% filter(resource == "gold"), aes(x = year, y = log(wb_price_mult), color = standardmeasure)) + geom_line() + labs(title="gold")

(using the full dataset) (使用完整的数据集)

Here is a way.这是一种方法。

Define two functions, in order to make the code more readable and, just in case, easier to debug.定义两个函数,以使代码更具可读性,以防万一,更易于调试。

  • A function to plot each resource;绘制每个资源的函数;
  • A function to save the plot to a PDF file.将绘图保存到 PDF 文件的功能。
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

Created on 2022-06-18 by the reprex package (v2.0.1)reprex 包于 2022-06-18 创建 (v2.0.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM