#### script for EDDIE Module on Global Carbon Budgets #### first step is to install the packages needed in this script #### run each line one time, on first use of that package, and then put a # in front of #### the command to make it a comment (which will not run again) install.packages('tidyverse') install.pacakges('plotly') ## install.packages downloads a package to your R library, but you have to ## use a separate command to activate that package within your R session ## the library() call for each package is required each time you start ## a new R session library(tidyverse) library(plotly) #### the next step is to read the data file into your R session #### download the data from the course web page and save it to a local directory on #### your computer (I recommend making a folder for this assignment on your computer) ### edit the line below to read in the datafile ### you will want to change the file path to match the location of the data on your computer setwd("~/Downloads/GlobalCarbonBudget/DataActivityFolder") data <- read_csv("globalcarbonbudget.csv") data2 <- gather(data, "SystemComponent", "GtonC", 2:7) view(data) view(data2) ## Part A: # Question 1 ### execute the code below to build your plot plot <- data2 %>% filter(SystemComponent == "fossil emissions") %>% ggplot(aes(Year, GtonC, color = SystemComponent)) + geom_path() + geom_point() + theme_bw() + theme(legend.position = "none") + ylab("Add your axis label (with units)") + xlab("") ggplotly(plot) # Question 2 ### below is an example of fitting a line to data for 1960-1980 ### follow along and then replicate that code to answer the remaining questions year_start = 1960 year_stop = 1980 data_lm <- data2 %>% filter(SystemComponent == "fossil emissions" & Year >= year_start & Year <= year_stop) linearmodel<- lm(GtonC ~ Year, data = data_lm) ### use to view summary of the model summary(linearmodel) ### will print out the slope of the line slope<- coef(linearmodel)[2] print(slope) # Question 3 ## In this question, you'll have to calculate a new variable to include in your plot ## I've done this for you here, so you have an example of a calculation ## read the code below and note where and how the new variable is calculated, you'll have ## to do something similar on your own in part C below plot_q3 <- data2 %>% filter(SystemComponent == "fossil emissions") %>% mutate(ppmCO2 = GtonC*(1/2.13)) %>% ## this line of code calculates the new variable ppmCO2 ggplot(aes(Year, ppmCO2)) + geom_col(fill = "cyan") + theme_bw() + theme(legend.position = "none") + ylab("Atmospheric CO2 equivalent (ppm)") + xlab("") ggplotly(plot_q3) ## Part B: # Question 4 plot_q4 <- data2 %>% ggplot(aes(Year, GtonC, color = SystemComponent)) + geom_path() + geom_point() + theme_bw() + theme() + scale_color_brewer(palette = "Spectral") + ylab("Add y-axis label here (with units)") + xlab("") ggplotly(plot_q4) # Question 5 plot_q5 <- data %>% mutate( total_emissions = `fossil emissions` + `land-use change emissions`, atmosphere = `atmospheric growth`/total_emissions*100, land = `land sink`/total_emissions*100, ocean = `ocean sink`/total_emissions*100 ) %>% gather("SystemComponent", "sink percent", 9:11) %>% ggplot(aes(Year, `sink percent`, color = SystemComponent)) + geom_path() + theme_bw() + theme() + geom_point() + scale_color_manual(values = c("light blue","dark green","dark blue")) + ylab("Add label (with units)") + xlab("") ggplotly(plot_q5) # Part C: # Question 6 plot_q6 <- data2 %>% filter(SystemComponent == "fossil emissions" | SystemComponent == "land-use change emissions") %>% group_by(Year) %>% summarize(Total_Emissions = sum(GtonC)) %>% ggplot(aes(Year, Total_Emissions)) + geom_path() + geom_point() + theme_bw() + theme(legend.position = "none") + ylab("Add label (with units)") + xlab("") ggplotly(plot_p6) # Question 7 ## you must chose a time period below to initiate the model (or you'll get an error) year_start_pC = YYYY year_stop_pC = YYYY data_lm_pC <- data2 %>% filter(SystemComponent == "fossil emissions excluding carbonation" | SystemComponent == "land-use change emissions") %>% group_by(Year) %>% summarize(Total_Emissions = sum(GtonC)) %>% filter(Year >= year_start_pC & Year <= year_stop_pC) linearmodel_p3 <- lm(Total_Emissions ~ Year, data = data_lm_pC) ### use to view summary of the model summary(linearmodel_p3) ### will print out the slope of the line slope<- coef(linearmodel_p3)[2] print(slope) ## Question 8 ## note you can use R as a calculator below NetEmissions2020 = XXXX ### fill in value here RateofDecrease = XXXX ### add the rate you are using here Yrs_to_NetZero = NetEmissions2020/RateofDecrease CalendarYear_NetZero = round(2020 + Yrs_to_NetZero) print(CalendarYear_NetZero) #this should print the calendar year you will hit net zero ## Question 9 ### Here is an adaptable template to update with your linear model and projection plot_q9 <- data2 %>% filter(SystemComponent == "fossil emissions" | SystemComponent == "land-use change emissions") %>% group_by(Year) %>% summarize(Total_Emissions = sum(GtonC)) %>% ggplot(aes(Year, Total_Emissions)) + geom_path() + geom_point() + theme_bw() + theme(legend.position = "none") + ylab("Add label (with units)") + xlab("") + ## update the XXX below to add values needed to add your projection in from part 8 geom_segment(aes(y = XXX, yend = 0, x = 2020, xend = XXX)) ## adding line for your projection ggplotly(plot_q9) ## Question 10 ## Rate of 2021 emissions is 10.3, goal is to cut to 5.15 in 8 years... ## use the space below to calculate as needed Rate_to_Net_Zero_2030 = XXXX/XXX