Assignment 8 Continuous Variables with DS Labs and Highcharter Tutorials
Author
Angel Porter
The echo: false option disables the printing of code (only output is displayed).
#Load the required packageslibrary(tidyverse) #for creating plots and graphs
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.0 ✔ readr 2.1.6
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.2 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr) #reading and writes CSV and other text fileslibrary("dslabs") #data wrangling/cleaning datalibrary(dplyr)#processing and manipulating datalibrary(tibble) #display is prettylibrary(viridis) #color palatte adjustments
# Continuous numerical variables are usually measured, such as height. These variables can take on an infinite number of values within a given range.# To produce informative graphics that tell a clear story, data journalists often need to turn a continuous variable into a categorical variable by dividing it into bins.# Take a peek at what the data look likehead(temp_carbon_df)
temp_tbl <-as_tibble(temp_carbon_df)# A heatmap visualizes a table of numbers by substituting the numbers with colored cells.# We will be using a cluster heatmap.# Identify names of each columnnames(temp_tbl)
# Identify missing values in each columncolSums(is.na(temp_tbl))
year temp_anomaly land_anomaly ocean_anomaly
0 129 129 129
carbon_emissions
4
heat_data <- temp_tbl %>%# Handle missing valuesdrop_na(year, temp_anomaly, carbon_emissions) %>%# Convert continuous variables into categorical bins for heatmap structuremutate(carbon_emissions_bin =cut(carbon_emissions, breaks =10),temp_anomaly_bin =cut(temp_anomaly, breaks =10) ) %>%# Group by bin categoriesgroup_by(carbon_emissions_bin, temp_anomaly_bin) %>%# Compute the average year for each bin combinationsummarize(avg_year =mean(year, na.rm =TRUE), .groups ="drop")# Graph a heatmapggplot(heat_data, aes(x = carbon_emissions_bin, y = temp_anomaly_bin, fill = avg_year)) +# Draw tiles representing each bin combinationgeom_tile(color ="grey70") +# Apply non-default color scale to represent average yearscale_fill_viridis_c(option ="cividis") +# Add labels and titlelabs(title ="Carbon Emissions vs Temperature Anomaly Colored by Year",subtitle ="Heatmap showing when combinations of emissions and temperature occurred",x ="Carbon Emissions (binned)",y ="Temperature Anomaly (binned)",fill ="Average Year",caption ="DS Labs" ) +# Change theme and rotate x-axis labels to prevent overlaptheme_dark() +theme(axis.text.x =element_text(angle =45, hjust =1))
#I decided to take a look at Global temperature anomaly and carbon emissions, 1751-2018. #The heatmap indicates a distinct upward trend in both carbon emissions and temperature anomalies over time. Earlier years correspond to lower emissions and temperature anomalies, whereas recent years exhibit higher values for both variables. This pattern demonstrates a strong positive relationship between carbon emissions and temperature changes, as both metrics increase concurrently over time.