I feel like this should be really simple, but I have been struggling with it. I used dplyr to get the summary statistics for the following tibble:
Scope <- data %>% group_by(Scope) %>% summarize(Emissions_2018 = sum(Emissions_2018),
Emissions_2019 = sum(Emissions_2019),
"% Change" = (sum(Emissions_2019) - sum(Emissions_2018)) / sum(Emissions_2018) * 100)
Scope
A tibble: 3 x 4
Scope `Emissions 2018` `Emissions 2019` `% Change`
<chr> <dbl> <dbl> <dbl>
1 Scope 1 421972. 418797. -0.752
2 Scope 2 304711. 281526. -7.61
3 Scope 3 52184490. 51729720. -0.871
Now, I'm trying to use ggplot to graph Emissions 2018, Emissions 2019, and % Change as you would see in pre- and post-treatment experiment. Year is my x-axis, GHG emissions is my y-axis, and % change should be the vertical line between the row observations (2018 to 2019). I've tried many different things, but I am stuck.
Edit (12/5/20):
So I followed through on what you suggested, Andrew. This looks good:
Scope <- read.table(text = "Scope Emissions2018 Emissions2019 Change
Scope_1 421972. 418797. -0.752
Scope_2 304711. 281526. -7.61
Scope_3 52184490. 51729720. -0.871",
header = TRUE)
dat <- Scope %>%
pivot_longer(Emissions2018:Emissions2019, names_to = "Year") %>%
mutate(across(Year, function(x) gsub("Emissions", "", x)))
# round to 3 significant figures and add percentage sign
label_df <- unique(dat[,c("Scope","Change")])
label_df$Change <- paste0(signif(label_df$Change, 3), "%")
p <- ggplot(dat, aes(x = Year, y = value))
p <- p + facet_wrap(. ~ Scope, ncol=3)
p <- p + geom_point()
p <- p + labs(y = "Emissions", x = "Year")
p <- p + geom_text(x = 1.5, y = 1000, aes(label = Change), data = label_df)
print(p)
However, the issue is that, when you actually produce the graphs, you get the following: Scope Emissions.
Because of the y-axis' scaling, the % change is very hard to highlight. I want to apply scaling similar to the code I see here.
My computer does not support the package facetscales, and I also believe that there should be a much simpler way to do this.
Any suggestions?