This section outlines the creation of the interactive ggplotly
graphs.
This code outlines the creation of each ggplotly graph. An initial
ggplot scatter plot, with a linear best fit line is constructed. The
scatter plot’s point colours match the Rcolorbrewer palette of the
leaflet map, to create a recognisable link between the visuals.
Labels are then added, with the custom theme ’gdocs’ and the Google
font ‘Source Sans Pro’ set. This styles the plot to have the
professional layout of google charts, and to fit within the dashboard’s
overall theming.
####### create ggplotly graphs ##################################
#### GDP ############################################
### ggplot ###
#create base graph
scatter_gdp <- ggplot(scatterdf, aes(x = gdp, y = happiness_score, label = Country)) + #adding text removes geom_smooth
geom_point(colour = '#005a32', alpha = 0.7, size = 2.5) +
geom_smooth(method = 'lm', se = FALSE, colour = 'gold2', alpha = 0.6, size = 1)
#add labels
scatter_gdp <- scatter_gdp +
labs(title = "Relationship Between a Country's GDP per Capita and Happiness",
x = "Gross Domestic Product per Capita (US$)",
y = "World Happiness Index Score")
#add theme
scatter_gdp <- scatter_gdp +
theme_gdocs() + #custom theme from ggtheme
theme(
text = element_text(family = "Source Sans Pro"),
plot.title = element_text(size = 16, face = 'bold'),
plot.subtitle = element_text(size = 14),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12))
## create ggplotly ##
graph_gdp.interactive = ggplotly(scatter_gdp, tooltip = 'Country')%>%
style(hoverlabel = labelplot) %>%
layout(
font = font,
xaxis = list(fixedrange = TRUE), #disables ability to scroll around plot
yaxis = list(fixedrange = TRUE,
title = list(x= 0, xanchor = 'left', xref = 'container'))) %>% #edits title position
config(displayModeBar = FALSE, showTips = FALSE) #disables fetures bar
#save graph
saveWidget(graph_gdp.interactive, here('graphs', "gdp_interactive.html"))
#### Population Density ############################################
## ggplot ##
#create base graph
scatter_pop <- ggplot(scatterdf, aes(x = pop_density, y = happiness_score, label = Country)) +
geom_point(colour = '#6a51a3', alpha = 0.7, size = 2.5) +
geom_smooth(method = 'lm', se = FALSE, colour = 'gold2', alpha = 0.6, size = 1)
#add labels
scatter_pop <- scatter_pop +
labs(title = "Relationship Between a Country\'s Population Density and Happiness",
x = "Population Density (km\u00B2)",
y = "World Happiness Index Score")
scatter_pop <- scatter_pop +
theme_gdocs() + #custom theme
theme(
text = element_text(family = "Source Sans Pro"),
plot.title = element_text(size = 16, face = 'bold'),
plot.subtitle = element_text(size = 14),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12))
## create ggplotly ##
graph_pop.interactive = ggplotly(scatter_pop, tooltip = 'Country')%>%
style(hoverlabel = labelplot) %>%
layout(font = font,
xaxis = list(fixedrange = TRUE),
yaxis = list(fixedrange = TRUE)) %>%
config(displayModeBar = FALSE, showTips = FALSE)
#save graph
saveWidget(graph_pop.interactive, here('graphs', "pop_interactive.html"))
#### Population Logged #########################################################
### ggplot ###
#create base graph
scatter_poplog <- ggplot(scatterdf, aes(x = pop_density_log, y = happiness_score, label = Country)) +
geom_point(colour = '#6a51a3', alpha = 0.7, size = 2.5) +
geom_smooth(method = 'lm', se = FALSE, colour = 'gold2', alpha = 0.6, size = 1)
#add labels
scatter_poplog <- scatter_poplog +
labs(title = "Relationship Between a Country\'s Population Density and Happiness \n Adjusted for Outliers (Log Transformed)",
x = "Logged Population Density (km\u00B2)",
y = "World Happiness Index Score")
#add theme
scatter_poplog <- scatter_poplog +
theme_gdocs() +
theme(
text = element_text(family = "Source Sans Pro"),
plot.title = element_text(size = 16, face = 'bold'),
plot.subtitle = element_text(size = 14),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12))
## create ggplotly ##
graph_poplog.interactive = ggplotly(scatter_poplog, tooltip = 'Country')%>%
style(hoverlabel = labelplot) %>%
layout(font = font,
xaxis = list(fixedrange = TRUE),
yaxis = list(fixedrange = TRUE)) %>%
config(displayModeBar = FALSE, showTips = FALSE)
#save graph
saveWidget(graph_poplog.interactive, here('graphs', "poplog_interactive.html"))
#### Covid #########################################
#create base graph
scatter_covid <- ggplot(scatterdf, aes(x = avg_covid_score, y = happiness_score, label = Country)) + #adding text removes geom_smooth
geom_point(colour = '#084594', alpha = 0.7, size = 2.5) +
geom_smooth(method = 'lm', se = FALSE, colour = 'gold2', alpha = 0.6, size = 1)
#add labels
scatter_covid <- scatter_covid +
labs(title = "Relationship Between a Country's Covid-19 Lockdown Severity and Happiness",
x = "Covid-19 Stringency Index Score \n (Higher scores Indicate more Extreme Measures)",
y = "World Happiness Index Score")
#add theme
scatter_covid <- scatter_covid +
theme_gdocs() +
theme(
text = element_text(family = "Source Sans Pro"),
plot.title = element_text(size = 16, face = 'bold'),
plot.subtitle = element_text(size = 14),
axis.title.x = element_text(size = 12),
axis.title.y = element_text(size = 12))
#create ggplotly
graph_covid.interactive = ggplotly(scatter_covid, tooltip = 'Country')%>%
style(hoverlabel = labelplot) %>%
layout(font = font,
xaxis = list(fixedrange = TRUE),
yaxis = list(fixedrange = TRUE)) %>%
config(displayModeBar = FALSE, showTips = FALSE)
#save graph
saveWidget(graph_covid.interactive, here('graphs', "covid_interactive.html"))