This page accompanies the article ‘The Relationship Between Age and Happiness Varies by Income’ published in Journal of Happiness Studies (Toshkov, 2021). The article is open access and uses the publicly-available data from the European Social Survey (ESS), which you can access here. You can find all the replication code in R
at this link. Here, I show some additional figures that visualize the main relationships I study in the article in color and using an interactive 3D plot.
The main purpose of the analysis is to explore how average levels of happiness in Europe vary as a function of the age and income of the respondent. The effects of age and income are considered simultaneously, so that they can interact: for example, the effect of age on happiness can be strong for the poor but relatively weak for the rich. To show how happiness varies by age and income, in the article I show this static 3D plot in block and white.
load (file = './data/3d-data.Rdata')
load (file = './data/3d-data2.Rdata')
#load (file = './data/3d-elevation-data.Rdata')
library(plot3D)
library(rgl)
library(plotly)
par(mfrow = c(1, 1), mar = c(0, 0, 0, 0))
persp3D(z = temp.data, theta = 45, phi = 10, facets = T, curtain = TRUE,
shade=0.6, col = ramp.col(c("white", "black")), border = "grey", colkey = FALSE, xlab="Age (scaled)", ylab="Income (scaled)", zlab="Happiness")
The same plot can be reproduced in color, which reinforces the perception of height.
par(mfrow = c(1, 1), mar = c(0, 0, 0, 0))
persp3D(z = temp.data, theta = 45, phi = 15, facets = T, curtain = TRUE,
shade=0.1, colkey = FALSE, xlab="Age (scaled)", ylab="Income (scaled)", zlab="Happiness", box=T, bty='b2',ticktype = "detailed")
Then, we can use the
ribbon3D()
function to change the appearance of the plot by marking more clearly the discrete nature of the income variable.
par(mfrow = c(1, 1), mar = c(0, 0, 0, 0))
ribbon3D(z = temp.data, theta = 45, phi = 15, facets = T, curtain = TRUE,
shade=0.1, colkey = FALSE, xlab="Age (scaled)", ylab="Income (scaled)", zlab="Happiness", box=T, bty='b2',ticktype = "detailed")
One disadvantage of all these plots is that they only show the data from one perspective. We can show multiple perspectives by making an animated version of the rotating plot or by producing an interactive version (with the plotly
library) where the user can choose the perspective of interest.