Country Comparison
The gapminder
dataset has data on life expectancy, population, and GDP per capita for 142 countries from 1952 to 2007. To get a glimpse of the dataframe, namely to see the variable names, variable types, etc., we use the glimpse
function. We also want to have a look at the first 20 rows of data.
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
head(gapminder, 20)
## # A tibble: 20 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## 11 Afghanistan Asia 2002 42.1 25268405 727.
## 12 Afghanistan Asia 2007 43.8 31889923 975.
## 13 Albania Europe 1952 55.2 1282697 1601.
## 14 Albania Europe 1957 59.3 1476505 1942.
## 15 Albania Europe 1962 64.8 1728137 2313.
## 16 Albania Europe 1967 66.2 1984060 2760.
## 17 Albania Europe 1972 67.7 2263554 3313.
## 18 Albania Europe 1977 68.9 2509048 3533.
## 19 Albania Europe 1982 70.4 2780097 3631.
## 20 Albania Europe 1987 72 3075321 3739.
# look at the first 20 rows of the dataframe
To produce two graphs of how life expectancy has changed over the years for the country
and the continent
I come from.
country_data <- gapminder %>%
filter(country == "China")
continent_data <- gapminder %>%
filter(continent == "Asia")
First, create a plot of life expectancy over time for the single country I chose. Map year
on the x-axis, and lifeExp
on the y-axis.
plot1 <- ggplot(data = country_data, mapping = aes(x = year, y = lifeExp))+
geom_point() +
geom_smooth(se = FALSE)+
NULL
plot1
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Next we need to add a title. Create a new plot, or extend plot1, using the labs()
function to add an informative title to the plot.
plot1<- plot1 +
labs(title = "China_life expextancy over time",
x = "year",
y = "lifeExp") +
NULL
plot1
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Secondly, produce a plot for all countries in the continent I come from.
ggplot(continent_data, mapping = aes(x =year , y = lifeExp, colour=country , group = country))+
geom_point()+
geom_smooth(se = FALSE) +
NULL
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Finally, using the original gapminder
data, produce a life expectancy over time graph, grouped (or faceted) by continent. We will remove all legends, adding the theme(legend.position="none")
in the end of our ggplot.
ggplot(data = gapminder , mapping = aes(x = year , y = lifeExp , colour=continent ))+
geom_point() +
geom_smooth(se = FALSE) +
facet_wrap(~continent) +
theme(legend.position="none") + #remove all legends
NULL
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Conclusion
For China, the life expectancy has been growing since 1952. However, the speed of the growth of life expectancy in China decreased in the late 1970s. I think this is because since 1978, China’s Reform and Opening-Up has begun, which has changed people’s lifestyle and living standards to a certain extent and brought about the changes in the speed of growth of life expectancy.
In Asia, the life expectancy in most countries has been growing since 1952. Since late 1960s, Japan has become the country with the highest life expectancy in Asia. Maybe it is because Japanese eats more healthily than people in other countries.
As for different continents, the life expectancy for countries in Europe varies less, and the life expectancy for countries in Africa and Asia varies more. One possible reason is that most of the countries in Europe are well developed, and there is significant difference in the development levels in countries in Africa and Asia, which results in the difference of life expectancy.