My starting point for work on this project was Portfolio Project 1, where I replicated and improved on a visualization of GDP per capita and meat supply per capita (Original Visualization: https://ourworldindata.org/grapher/meat-consumption-vs-gdp-per-capita). The two pieces of interactivity that I added to my improved plot from Portfolio 1 were hovering with plotly and a sliderInput inside of a Shiny app. My plotly hovering allows users to see the country of a specific point, the meat supply per capita for that country, and the GDP per capita of that country. This is a huge improvement over my original static plot, which can be seen below. In my previous plot, it was impossible to know which country corresponded to which point based on the plot. This is valuable information about the data that my previous plot simply did not show. For instance, people may want to further investigate countries that fall far away from the trend line, countries with the highest or lowest GDP per capita, or countries with the highest or lowest meat supply per capita. Also, in my previous plot, it is difficult to tell exactly what a country’s GDP per capita or meat supply per capita is. Hence, allowing users to see this information when they hover better helps them understand the values that different points have and conceptualize the data better. The slider that I added allows for users to compare the GDP per capita versus meat supply per capita trends between years in a way that does not sacrifice the size of each unique plot. In contrast, my previous plot allowed for users to compare years simultaneously by displaying them in a faceted manner. However, each unique plot was very small and it was difficult to interpret much outside of the trend lines and seeing that there were some points that far away from the trend lines. Adding the slider also allowed for me to include more years of data, as I included data from each year from 1990 to 2022. I did not do this in the previous plot because 33 facets can be difficult to look at, so I included seven evenly spaced out years of data. So, the slider improves the visualization because it allows users to compare years without sacrificing the size and interpretability of individual plots and allows me to include more years of data without worrying about having too many plots on the screen at once or having individual plots be too small. Despite this, the slider alone does not address how it is easier (at least to me) to compare different plots when you look at them next to each other rather than having to go back and forth with a slider. This is the reason I decided to create a plot faceted by year in the first place in Portfolio Project 1. To account for this, there are two plots that each correspond to their own slider in the Shiny app, which allows users to look at and compare two years of data simulataneuously.
meat_and_gdp <- readr::read_csv("meatGDP2022.csv") |>
filter(!str_detect(Entity, "European") & #Remove European Union
!str_detect(Entity, "High-income") & #Remove "High Income" Entity
!str_detect(Entity, "Low-income") & #Remove "Low-Income" Entity
!str_detect(Entity, "Lower-middle") & #Remove "Lower-middle"
!str_detect(Entity, "Upper-middle") & #Remove "Upper-middle"
!str_detect(Entity, "World")) #Remove "World" Entity
colnames(meat_and_gdp)[4] <- "Meat_Supply"
colnames(meat_and_gdp)[5] <- "GDP_per_capita"
colnames(meat_and_gdp)[6] <- "world_region"
year_list = seq(1990, 2021)
meat_gdp_final <- meat_and_gdp
for(i in year_list){
csv_file <- str_c("meatGDP",i,".csv")
current_meat_gdp <- read_csv(csv_file) |>
filter(!str_detect(Entity, "European") & #Remove European Union
!str_detect(Entity, "High-income") &
!str_detect(Entity, "Low-income") &
!str_detect(Entity, "Lower-middle") &
!str_detect(Entity, "Upper-middle") &
!str_detect(Entity, "World"))
colnames(current_meat_gdp)[4] <- "Meat_Supply"
colnames(current_meat_gdp)[5] <- "GDP_per_capita"
colnames(current_meat_gdp)[6] <- "world_region"
meat_gdp_final <- bind_rows(meat_gdp_final, current_meat_gdp)
}
ggplot(data = meat_and_gdp) +
geom_point(mapping = aes(x = GDP_per_capita, y = Meat_Supply, color = world_region)) +
scale_x_continuous(transform = "log2", breaks = c(1000, 2000, 5000, 10000, 20000, 50000, 100000), labels = c("$1,000", "$2,000", "$5,000", "$10,000", "$20,000", "$50,000", "$100,000")) +
scale_y_continuous(breaks = c(0, 20, 40, 60, 80, 100, 120, 140), labels = c("0 kg", "20 kg", "40 kg", "60 kg", "80 kg", "100 kg", "120 kg", "140 kg"))+
labs(x = "GDP per capita (international-$ in 2021 prices)", y = "", color = "") +
scale_color_brewer(palette = "Dark2") +
ggtitle("Meat Supply per person (kilograms per year per capita)") +
theme_minimal() +
theme(panel.grid = element_line(color = "gray", size = 0.4, linetype = 2), panel.grid.minor = element_blank(), axis.line.x = element_line(colour = "grey50", linewidth = 1))+
guides(color= guide_legend(override.aes = list(shape = 15, size = 3)))
meat_and_gdp_2017 <- read_csv("meatGDP2017.csv") #2017 Data
meat_and_gdp2 <- bind_rows(meat_and_gdp, meat_and_gdp_2017)
meat_and_gdp_2012 <- read_csv("meatGDP2012.csv") #2012 Data
meat_and_gdp2 <- bind_rows(meat_and_gdp2, meat_and_gdp_2012)
meat_and_gdp_2007 <- read_csv("meatGDP2007.csv") #2007 Data
meat_and_gdp2 <- bind_rows(meat_and_gdp2, meat_and_gdp_2007)
meat_and_gdp_2002 <- read_csv("meatGDP2002.csv") #2002 Data
meat_and_gdp2 <- bind_rows(meat_and_gdp2, meat_and_gdp_2002)
meat_and_gdp_1997 <- read_csv("meatGDP1997.csv") #1997 Data
meat_and_gdp2 <- bind_rows(meat_and_gdp2, meat_and_gdp_1997)
meat_and_gdp_1992 <- read_csv("meatGDP1992.csv") #1992 Data
meat_and_gdp2 <- bind_rows(meat_and_gdp2, meat_and_gdp_1992)
ggplot(data = meat_and_gdp2) +
geom_point(mapping = aes(x = Meat_Supply, y = GDP_per_capita, color = world_region, text = paste(Entity, "\n", "Meat Supply Per Capita:", Meat_Supply, "kg", "\n", "GDP Per Capita: $",GDP_per_capita))) +
facet_wrap(~Year) +
scale_y_continuous(breaks = c(20000, 50000, 100000), labels = c("$20,000", "$50,000", "$100,000")) +
scale_x_continuous(breaks = c(40, 80, 120), labels = c("40 kg", "80 kg", "120 kg"))+
labs(x = "Meat Supply per person (kilograms per year per capita)", y = "", color = "") +
geom_smooth(data = meat_and_gdp2, mapping = aes(x = Meat_Supply, y = GDP_per_capita), method = "lm", se = FALSE, color = "blue") +
scale_color_brewer(palette = "Dark2") +
ggtitle("GDP Per Capita by Meat Supply") +
theme_minimal() +
theme(panel.grid = element_line(color = "gray", size = 0.4, linetype = 2), panel.grid.minor = element_blank(), axis.line.x = element_line(colour = "grey50", linewidth = 0.5)) +
guides(color= guide_legend(override.aes = list(shape = 15, size = 3)))
faceted_gdp_meat_plot <- ggplot(data = meat_and_gdp) +
geom_point(mapping = aes(x = Meat_Supply, y = GDP_per_capita, color = world_region, text = paste(Entity, "\n", "Meat Supply Per Capita:", Meat_Supply, "kg", "\n", "GDP Per Capita: $",GDP_per_capita))) +
scale_y_continuous(breaks = c(20000, 50000, 100000), labels = c("$20,000", "$50,000", "$100,000")) +
scale_x_continuous(breaks = c(40, 80, 120), labels = c("40 kg", "80 kg", "120 kg"))+
labs(x = "Meat Supply per person (kilograms per year per capita)", y = "", color = "") +
geom_smooth(data = meat_and_gdp2, mapping = aes(x = Meat_Supply, y = GDP_per_capita), method = "lm", se = FALSE, color = "blue") +
scale_color_brewer(palette = "Dark2") +
ggtitle("GDP Per Capita by Meat Supply") +
theme_minimal() +
theme(panel.grid = element_line(color = "gray", size = 0.4, linetype = 2), panel.grid.minor = element_blank(), axis.line.x = element_line(colour = "grey50", linewidth = 0.5)) +
guides(color= guide_legend(override.aes = list(shape = 15, size = 3)))
ggplotly(faceted_gdp_meat_plot, tooltip = "text")