Rene Kesenheimer
February 28, 2025
# Create sample happiness data (inspired by World Happiness Report)
set.seed(123)
countries <- c("Finland", "Denmark", "Switzerland", "Iceland", "Netherlands",
"Norway", "Sweden", "Luxembourg", "New Zealand", "Austria",
"Australia", "Israel", "Germany", "Canada", "Ireland",
"United States", "United Kingdom", "Czech Republic", "Belgium", "France")
happiness_data <- data.frame(
Country = countries,
HappinessScore = runif(20, 5.5, 8.0),
GDP = runif(20, 0.8, 2.0),
SocialSupport = runif(20, 0.8, 1.8),
LifeExpectancy = runif(20, 0.5, 1.5),
Freedom = runif(20, 0.4, 0.8),
Generosity = runif(20, 0.1, 0.5),
Corruption = runif(20, 0.1, 0.6),
Region = sample(c("Western Europe", "North America", "Oceania", "Middle East"), 20, replace = TRUE)
)# Create an interactive bubble chart
plot_ly(happiness_data,
x = ~GDP,
y = ~SocialSupport,
size = ~HappinessScore,
color = ~Region,
text = ~Country,
hoverinfo = "text",
hovertext = ~paste("Country:", Country,
"<br>Happiness Score:", round(HappinessScore, 2),
"<br>GDP per capita:", round(GDP, 2),
"<br>Social Support:", round(SocialSupport, 2)),
marker = list(opacity = 0.7, sizemode = "diameter")) %>%
layout(autosize = TRUE,
margin = list(l = 50, r = 50, b = 100, t = 80, pad = 4),
title = list(text = "Factors Contributing to National Happiness", font = list(size = 20)),
xaxis = list(title = "GDP per Capita"),
yaxis = list(title = "Social Support"),
legend = list(x = 0.85, y = 0.9),
hovermode = "closest")# Create a grouped box plot by region
plot_ly(happiness_data, y = ~HappinessScore, color = ~Region, type = "box") %>%
layout(autosize = TRUE,
margin = list(l = 50, r = 50, b = 100, t = 80, pad = 4),
title = list(text = "Happiness Score Distribution by Region", font = list(size = 20)),
xaxis = list(title = ""),
yaxis = list(title = "Happiness Score"))# Calculate correlation matrix
cor_matrix <- cor(happiness_data[, c("HappinessScore", "GDP", "SocialSupport",
"LifeExpectancy", "Freedom", "Generosity", "Corruption")])
# Create correlation heatmap
plot_ly(
z = cor_matrix,
x = colnames(cor_matrix),
y = colnames(cor_matrix),
type = "heatmap",
colorscale = "Viridis",
zmin = -1,
zmax = 1
) %>%
layout(autosize = TRUE,
margin = list(l = 100, r = 50, b = 100, t = 80, pad = 4),
title = list(text = "Correlation Between Happiness Factors", font = list(size = 20)),
xaxis = list(title = ""),
yaxis = list(title = ""))