library(dslabs) #contains gapminderlibrary(dplyr) #manipulating data
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(highcharter) #making interactive charts
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Highcharts (www.highcharts.com) is a Highsoft software product which is
not free for commercial and Governmental use
Attaching package: 'highcharter'
The following object is masked from 'package:dslabs':
stars
library(RColorBrewer) #palettedata(gapminder) #load data
Observe gapminder variables
str(gapminder) #observed 9 variables
'data.frame': 10545 obs. of 9 variables:
$ country : Factor w/ 185 levels "Albania","Algeria",..: 1 2 3 4 5 6 7 8 9 10 ...
$ year : int 1960 1960 1960 1960 1960 1960 1960 1960 1960 1960 ...
$ infant_mortality: num 115.4 148.2 208 NA 59.9 ...
$ life_expectancy : num 62.9 47.5 36 63 65.4 ...
$ fertility : num 6.19 7.65 7.32 4.43 3.11 4.55 4.82 3.45 2.7 5.57 ...
$ population : num 1636054 11124892 5270844 54681 20619075 ...
$ gdp : num NA 1.38e+10 NA NA 1.08e+11 ...
$ continent : Factor w/ 5 levels "Africa","Americas",..: 4 1 1 2 2 3 2 5 4 3 ...
$ region : Factor w/ 22 levels "Australia and New Zealand",..: 19 11 10 2 15 21 2 1 22 21 ...
Filter data
#observe only the "big 4"big4 <- gapminder |>filter(country %in%c("China","Germany", "Japan", "United States")) |>arrange(year)
#head(big4)
Exploring life expectancy
Interactive line chart
#Variable for non-default colorcols <-brewer.pal(8,"Set2")#Line chart typehighchart () |>hc_add_series(data = big4,type ="line",hcaes(x = year,y = life_expectancy, group = country)) |>hc_add_theme(hc_theme_ggplot2(size =4)) |>#changed default theme using "Help"hc_colors(cols) |>#Referenced Highcharter in Rhc_title(text ="Life Expectancy of The Big 4") |>hc_caption(text ="Data Source: DS Labs", align ="right") |>hc_xAxis(title =list(text="Year")) |>hc_yAxis(title =list(text="Life expectancy (age)")) |>hc_legend(align ="right", verticalAlign ="top",layout ="vertical")
Conclusion
Visualization
In this visualization I used the gapminder dataset from DS Labs. My graph represents the changes in life expectancy of China, Germany, Japan, and the United States as time passes. I executed this by filtering the original gapminder data into only representing the life expectancy of a few countries with the largest economies in the world, named by the variable “Big4”. In observing the big four, highchart() was the best for representing life expectancy overtime. From analyzing my interactive line chart, I found that there wasn’t a great variation in life expectancy, except for an obvious economic boom experienced by China.
Technical Challenges and Future Studies
I have few technical challenges when using highchart() such as incorporating the title, caption, and theme. However, I did primarily struggle with choosing how to represent the data. Contrary to class notes, I could not use an area chart as observing the changes in life expectancy would be difficult as there was no great variation in values among the big four. If I were to continue exploring this data, I would like to analyze the correlation between a countries gross domestic product (GDP) and life expectancy.