Import Your Data

In the following code chunk, import your data.

#### Use read_csv() or another function

#### Make sure your data is converted into a tibble. 

dat <- read_csv("C:/Users/ameer/desktop/data/all.csv")
## Parsed with column specification:
## cols(
##   country = col_character(),
##   year = col_double(),
##   life_ladder = col_double(),
##   log_gdp_per_capita = col_double(),
##   social_support = col_double(),
##   healthy_life_expectancy_at_birth = col_double(),
##   freedom_to_make_life_choices = col_double(),
##   generosity = col_double(),
##   perceptions_of_corruption = col_double(),
##   positive_affect = col_double(),
##   negative_affect = col_double(),
##   trust = col_double(),
##   hdi = col_double(),
##   factorized_hdi = col_character()
## )
dat$factorized_hdi <- factor(dat$factorized_hdi, levels = c("High HDI", "Medium HDI", "Low HDI"))

dat
## # A tibble: 1,949 x 14
##    country   year life_ladder log_gdp_per_cap~ social_support healthy_life_expe~
##    <chr>    <dbl>       <dbl>            <dbl>          <dbl>              <dbl>
##  1 Afghani~  2008        3.72             7.37          0.451               50.8
##  2 Afghani~  2009        4.40             7.54          0.552               51.2
##  3 Afghani~  2010        4.76             7.65          0.539               51.6
##  4 Afghani~  2011        3.83             7.62          0.521               51.9
##  5 Afghani~  2012        3.78             7.70          0.521               52.2
##  6 Afghani~  2013        3.57             7.72          0.484               52.6
##  7 Afghani~  2014        3.13             7.72          0.526               52.9
##  8 Afghani~  2015        3.98             7.70          0.529               53.2
##  9 Afghani~  2016        4.22             7.70          0.559               53  
## 10 Afghani~  2017        2.66             7.70          0.491               52.8
## # ... with 1,939 more rows, and 8 more variables:
## #   freedom_to_make_life_choices <dbl>, generosity <dbl>,
## #   perceptions_of_corruption <dbl>, positive_affect <dbl>,
## #   negative_affect <dbl>, trust <dbl>, hdi <dbl>, factorized_hdi <fct>

Part 1

For my first figure, I am going to create a scatterplot that plots GDP(log) per capita in the x-axis against self-reported happiness (surveyed by the name “life_ladder” as a scale from 0 to 10, where 10 is happiest). I’ll also add a third variable in the form of color, which will represent self-reported social support. A loess line will be introduced.

dat %>% ggplot(aes(x=log_gdp_per_capita, y=life_ladder, col=social_support)) + geom_point(alpha=0.7) + geom_smooth()+ scale_color_continuous(type = "viridis") + labs(title="Scatterplot for worldwide Happiness, GDP and Social Support", x="GDP(log) per capita", y="Self-reported happiness (out of 10)", col = "Self-reported Social Support")
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 36 rows containing non-finite values (stat_smooth).
## Warning: Removed 36 rows containing missing values (geom_point).

Part 2

For my second figure, I am going to create a boxplot that includes three elements for different HDI values in the x-axis, and self-reported happiness in the y-axis. I’ll also paint the different HDI values with unique colors and hide the guide.

dat$factorized_hdi <- factor(dat$factorized_hdi, levels = c("Low HDI","Medium HDI", "High HDI"))

dat %>% drop_na(factorized_hdi) %>% ggplot(aes(x=factorized_hdi, y=life_ladder, fill=factorized_hdi)) + geom_boxplot() + theme(legend.position = "none") + labs(title="Boxplot for Happiness according to HDI", x="HDI (Discrete)", y="Self-reported Happiness")

Part 3

For the third figure, I’m going to plot a line showing changes in self-reported “Freedom of Life Choices” worldwide. A higher value indicates more perceived freedoms. I’ll choose a theme, and make “Tufte”, and change color and size of line to “lightblue” and 2 respectively.

dat %>% group_by(year) %>% summarise(freedom_life_choices = mean(freedom_to_make_life_choices, na.rm=TRUE)) %>% ggplot(aes(x=year, y=freedom_life_choices)) + geom_line(col="lightblue", size=2) + theme_tufte() + labs(title="Worldwide percieved freedom levels across the years", x="Year", y="Precieved Freedom to Make Life Choices")

For the Fourth graph, I’m going to show correlations of all my continuous variables using ggpairs, in which I’m going to customize the interface, showing red histograms on the diagonal, and a linear regression line for the scatterplot.

Part 4

my_hist <- function(data, mapping){
  ggplot(data=data, mapping=mapping) + geom_histogram(alpha=0.7, fill="red")
}
my_points <- function(data, mapping){
  ggplot(data=data, mapping=mapping) + geom_point(alpha=0.3) + geom_smooth(method="lm")
}

ggpairs(select(dat, -c(country, factorized_hdi)),
        lower=list(continuous=my_points),
        diag=list(continuous=my_hist), title = "Correlation Matrix for alll Continuous Variables in the Data")

Part 5

An interactive plot showing the relationship between trusting others and self-reported happiness for different countries in 2014. Scrolling to individual points shows the names of their corresponding countries.

plotly::ggplotly(dat %>% filter(year==2014) %>% drop_na(trust) %>% ggplot(aes(trust, life_ladder, col=factorized_hdi, id=country)) + 
  geom_point() + labs(title="Relationship between Trust-to-Others & Happiness broken down by HDI in 2014",
                      x="Trust-to-Others", y="Self-reported Happiness", col="HDI Level"))

Part 6

I’ll animate the changes in the relationship between social support and happiness throughout the years, and also showing the changes in GDP and HDI levels.

animate(drop_na(dat, factorized_hdi) %>% ggplot(aes(x=social_support, y=life_ladder, fill=factorized_hdi, size=log_gdp_per_capita)) +
          geom_point(aes(group = seq_along(year)), alpha=0.5, shape=21, color="black") + 
          scale_size(range=c(0.1,24)) +
          guides(fill = guide_legend(override.aes = list(size=10)))+
          scale_fill_viridis(discrete=TRUE, option="A")+
          theme_ipsum(axis_title_size = 21) +
          transition_time(as.integer(year)) +
          labs(title = 'Year: {frame_time}', size = 'GDP (log) per capita', fill = "HDI Level", x = "Self-reported Social Support", y = 'Self-reported Happiness'),
        height = 960,
        width = 1280,
        nframes = 60
)

Part 7

In the 7th figures, I’m going to make a simple animation for the worldwide change of happiness levels throughout the years.

dat %>% group_by(year) %>% summarise(happiness = mean(life_ladder, na.rm=TRUE)) %>% ggplot(aes(x=year, y=happiness)) + geom_line(col="darkcyan")+ geom_point()+ labs(title="Changes in Worldwide Self-reported Happiness Levels throughout the Years", x="Year", y="Self-reported Happiness") + transition_reveal(as.integer(year))

Part 8

For the last figure, I’m going to make a dumbbell chart, showing the changes in trust between the years 2009 and 2014 for a set of countries.

g <- dat %>% filter(year %in% c(2009, 2014)) %>% select(trust, year, country, factorized_hdi) %>% pivot_wider(names_from = year, values_from = trust) %>% drop_na()

g %>% ggplot(aes(x=`2009`, xend=`2014`, y=country)) + geom_dumbbell (color = "palevioletred4", colour_x="burlywood", colour_xend = "white", size = 1.2, size_x =3 , size_xend = 3) + labs(title="Dumbbell chart showing changes in Trust-to-others Attitude for a Set of Countries between 2009 and 2014", subtitle = "White represents the year 2014, while sepia-ish brown represents 2009", x= "Trust-to-others levels", y="Country")