2021年5月5日星期三

Adding linear trend lines using subsets of data to a time series graph in ggplot2

I am doing research on the pandemic on Net Sales figures of companies in different industries. For this I have a dataset containing Net Sales figures of companies of the different industries. Now I would like to create plots per industry on one graph with 1 line corresponding to the aggregated Net Sales per year (from 2010-2020), and the other being a trend line from 2010-2019 onto 2020 (so the expected Net Sales for 2020 taking the previous years into account). This way I have a visual aid to see whether 2020 has seen significantly worse numbers. I have gotten the first graphs (aggregated Net Sales per year per industry) sorted using dplyr with:

library(ggplot); library(tidyverse)  Industries <- df %>%        group_by(NAICS, Year) %>%        summarize(Sales = mean(`Net Sales`))        Industry_Plot <- ggplot(data = Industries, aes(Year, Sales)) +    theme_bw() +    theme(axis.text.y = element_blank(),          axis.ticks.y = element_blank(),          panel.grid = element_blank()) +    geom_line(color = "steelblue", size = 1)  

Now for the second part, I need to know:

  1. How to graph the trend line per industry for 2020, and
  2. How can I combine these onto 1 graph.

I have included a 'dput()' sample of my dataset for one industry below (these are the aggregated Net Sales for one industry (NAICS):

Industries <- structure(list(NAICS = c(315, 315, 315, 315, 315, 315, 315, 315, 315, 315, 315),       Year = c(2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020),       Sales = c(1839.94, 2227.27, 2203.9, 2421.88, 2681.7, 2789.2, 2834.07, 2944.34, 3129.65, 3213.13, 2757.85)),       row.names = c(NA, -11L),       groups = structure(list(NAICS = 315, .rows = structure(list(1:11),       ptype = integer(0),       class = c("vctrs_list_of", "vctrs_vctr", "list"))),       row.names = 1L, class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE),       class = c("grouped_df", "tbl_df", "tbl", "data.frame"))  

I am at a total loss. Thanks in advance for helping me.

https://stackoverflow.com/questions/67400117/adding-linear-trend-lines-using-subsets-of-data-to-a-time-series-graph-in-ggplot May 05, 2021 at 07:09PM

没有评论:

发表评论