Home > front end >  Several line plots by group in one PDF
Several line plots by group in one PDF

Time:06-18

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.

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 = ".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 = ".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 = ".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 = ".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)

Each plot should show the log-prices (wb_price_mult) for one resource over the years for each measurement (standardmeasure). At the end I would like to have on PDF with all graphs.

Each graph should look something like: Example graph from full dataset for resource gold

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)

CodePudding user response:

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

  • Related