The Gapminder Foundation (www.gapminder.org) is a non-profit organization based in Sweden that promotes global development through the use of statistics that can help reduce misconceptions about global development.
Using ggplot and the points layer, create a scatter plot of life expectancy versus fertility for the African continent in 2012.
Remember that you can use the R console to explore the gapminder dataset to figure out the names of the columns in the dataframe.
In this exercise we provide parts of code to get you going. You need to fill out what is missing. But note that going forward, in the next exercises, you will be required to write most of the code.
library(dplyr)
##
## 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(ggplot2)
library(dslabs)
data(gapminder)
## fill out the missing parts in filter and aes
gapminder %>% filter(continent == "Africa" & year == 2012) %>%
ggplot(aes(fertility,life_expectancy)) +
geom_point()
Note that there is quite a bit of variability in life expectancy and fertility with some African countries having very high life expectancies. There also appear to be three clusters in the plot.
Remake the plot from the previous exercises but this time use color to distinguish the different regions of Africa to see if this explains the clusters. Remember that you can explore the gapminder data to see how the regions of Africa are labeled in the data frame!
Use color rather than col inside your ggplot call - while these two forms are equivalent in R, the grader specifically looks for color.
library(dplyr)
library(ggplot2)
library(dslabs)
data(gapminder)
gapminder %>%
filter(continent == "Africa", year == 2012) %>%
ggplot(aes(fertility, life_expectancy, color = region )) +
geom_point()
While many of the countries in the high life expectancy/low fertility cluster are from Northern Africa, three countries are not.
Create a table showing the country and region for the African countries (use select) that in 2012 had fertility rates of 3 or less and life expectancies of at least 70.
Assign your result to a data frame called df.
library(dplyr)
library(dslabs)
data(gapminder)
df <- gapminder %>%
filter(continent == "Africa" & year == 2012 & fertility <= 3 & life_expectancy >= 70) %>%
select(country, region)
df
## country region
## 1 Algeria Northern Africa
## 2 Cape Verde Western Africa
## 3 Egypt Northern Africa
## 4 Libya Northern Africa
## 5 Mauritius Eastern Africa
## 6 Morocco Northern Africa
## 7 Seychelles Eastern Africa
## 8 Tunisia Northern Africa
The Vietnam War lasted from 1955 to 1975. Do the data support war having a negative effect on life expectancy? We will create a time series plot that covers the period from 1960 to 2010 of life expectancy for Vietnam and the United States, using color to distinguish the two countries. In this start we start the analysis by generating a table.
Use filter to create a table with data for the years from 1960 to 2010 in Vietnam and the United States.
Save the table in an object called tab.
library(dplyr)
library(dslabs)
data(gapminder)
countries <- c("Vietnam", "United States")
tab <- gapminder %>%
filter(country %in% countries, year >= 1960 & year <= 2010)
tab
## country year infant_mortality life_expectancy fertility population
## 1 United States 1960 25.9 69.91 3.67 186176524
## 2 Vietnam 1960 75.6 58.52 6.35 32670623
## 3 United States 1961 25.4 70.32 3.63 189077076
## 4 Vietnam 1961 72.6 59.17 6.39 33666768
## 5 United States 1962 24.9 70.21 3.48 191860710
## 6 Vietnam 1962 69.9 59.82 6.43 34684164
## 7 United States 1963 24.4 70.04 3.35 194513911
## 8 Vietnam 1963 67.3 60.42 6.45 35722092
## 9 United States 1964 23.8 70.33 3.22 197028908
## 10 Vietnam 1964 61.7 60.95 6.46 36780984
## 11 United States 1965 23.3 70.41 2.93 199403532
## 12 Vietnam 1965 60.7 61.32 6.48 37860014
## 13 United States 1966 22.7 70.43 2.71 201629471
## 14 Vietnam 1966 59.9 61.36 6.49 38959335
## 15 United States 1967 22.0 70.76 2.56 203713082
## 16 Vietnam 1967 59.0 61.06 6.49 40074695
## 17 United States 1968 21.3 70.42 2.47 205687611
## 18 Vietnam 1968 58.2 60.45 6.49 41195833
## 19 United States 1969 20.6 70.66 2.46 207599308
## 20 Vietnam 1969 57.3 59.63 6.49 42309662
## 21 United States 1970 19.9 70.92 2.46 209485807
## 22 Vietnam 1970 56.4 58.78 6.47 43407291
## 23 United States 1971 19.1 71.24 2.27 211357912
## 24 Vietnam 1971 55.5 58.17 6.42 44485910
## 25 United States 1972 18.3 71.34 2.01 213219515
## 26 Vietnam 1972 54.7 58.00 6.35 45549487
## 27 United States 1973 17.5 71.54 1.87 215092900
## 28 Vietnam 1973 53.8 58.35 6.25 46604726
## 29 United States 1974 16.7 72.08 1.83 217001865
## 30 Vietnam 1974 52.8 59.23 6.13 47661770
## 31 United States 1975 16.0 72.68 1.77 218963561
## 32 Vietnam 1975 51.8 60.54 5.97 48729397
## 33 United States 1976 15.2 72.99 1.74 220993166
## 34 Vietnam 1976 50.9 62.07 5.80 49808071
## 35 United States 1977 14.5 73.38 1.78 223090871
## 36 Vietnam 1977 49.8 63.58 5.61 50899504
## 37 United States 1978 13.8 73.58 1.75 225239456
## 38 Vietnam 1978 48.8 64.86 5.42 52015279
## 39 United States 1979 13.2 74.03 1.80 227411604
## 40 Vietnam 1979 47.8 65.84 5.23 53169674
## 41 United States 1980 12.6 73.93 1.82 229588208
## 42 Vietnam 1980 46.8 66.49 5.05 54372518
## 43 United States 1981 12.1 74.36 1.81 231765783
## 44 Vietnam 1981 45.8 66.86 4.87 55627743
## 45 United States 1982 11.7 74.65 1.81 233953874
## 46 Vietnam 1982 44.8 67.10 4.69 56931822
## 47 United States 1983 11.2 74.71 1.78 236161961
## 48 Vietnam 1983 43.9 67.30 4.52 58277391
## 49 United States 1984 10.9 74.81 1.79 238404223
## 50 Vietnam 1984 43.0 67.51 4.36 59653092
## 51 United States 1985 10.6 74.79 1.84 240691557
## 52 Vietnam 1985 42.0 67.77 4.21 61049370
## 53 United States 1986 10.4 74.87 1.84 243032017
## 54 Vietnam 1986 41.0 68.07 4.06 62459557
## 55 United States 1987 10.2 75.01 1.87 245425409
## 56 Vietnam 1987 40.0 68.38 3.93 63881296
## 57 United States 1988 10.0 75.02 1.92 247865202
## 58 Vietnam 1988 38.9 68.68 3.81 65313709
## 59 United States 1989 9.7 75.10 2.00 250340795
## 60 Vietnam 1989 37.7 69.00 3.68 66757401
## 61 United States 1990 9.4 75.40 2.07 252847810
## 62 Vietnam 1990 36.6 69.30 3.56 68209604
## 63 United States 1991 9.1 75.50 2.06 255367160
## 64 Vietnam 1991 35.4 69.60 3.42 69670620
## 65 United States 1992 8.8 75.80 2.04 257908206
## 66 Vietnam 1992 34.3 69.80 3.26 71129537
## 67 United States 1993 8.5 75.70 2.02 260527420
## 68 Vietnam 1993 33.1 70.10 3.07 72558986
## 69 United States 1994 8.2 75.80 2.00 263301323
## 70 Vietnam 1994 32.0 70.30 2.88 73923849
## 71 United States 1995 8.0 75.90 1.98 266275528
## 72 Vietnam 1995 30.9 70.60 2.68 75198975
## 73 United States 1996 7.7 76.30 1.98 269483224
## 74 Vietnam 1996 29.9 70.90 2.48 76375677
## 75 United States 1997 7.5 76.60 1.97 272882865
## 76 Vietnam 1997 28.9 71.10 2.31 77460429
## 77 United States 1998 7.3 76.80 2.00 276354096
## 78 Vietnam 1998 27.9 71.50 2.17 78462888
## 79 United States 1999 7.2 76.90 2.01 279730801
## 80 Vietnam 1999 27.0 71.70 2.06 79399708
## 81 United States 2000 7.1 76.90 2.05 282895741
## 82 Vietnam 2000 26.1 72.00 1.98 80285563
## 83 United States 2001 7.0 76.90 2.03 285796198
## 84 Vietnam 2001 25.3 72.20 1.94 81123685
## 85 United States 2002 6.9 77.10 2.02 288470847
## 86 Vietnam 2002 24.6 72.50 1.92 81917488
## 87 United States 2003 6.8 77.30 2.05 291005482
## 88 Vietnam 2003 23.9 72.80 1.91 82683039
## 89 United States 2004 6.9 77.60 2.06 293530886
## 90 Vietnam 2004 23.2 73.00 1.90 83439812
## 91 United States 2005 6.8 77.60 2.06 296139635
## 92 Vietnam 2005 22.6 73.30 1.90 84203817
## 93 United States 2006 6.7 77.80 2.11 298860519
## 94 Vietnam 2006 22.0 73.50 1.89 84979667
## 95 United States 2007 6.6 78.10 2.12 301655953
## 96 Vietnam 2007 21.4 73.80 1.88 85770717
## 97 United States 2008 6.5 78.30 2.07 304473143
## 98 Vietnam 2008 20.8 74.10 1.86 86589342
## 99 United States 2009 6.4 78.50 2.00 307231961
## 100 Vietnam 2009 20.3 74.30 1.84 87449021
## 101 United States 2010 6.3 78.80 1.93 309876170
## 102 Vietnam 2010 19.8 74.50 1.82 88357775
## gdp continent region
## 1 2.479391e+12 Americas Northern America
## 2 NA Asia South-Eastern Asia
## 3 2.536417e+12 Americas Northern America
## 4 NA Asia South-Eastern Asia
## 5 2.691139e+12 Americas Northern America
## 6 NA Asia South-Eastern Asia
## 7 2.809549e+12 Americas Northern America
## 8 NA Asia South-Eastern Asia
## 9 2.972502e+12 Americas Northern America
## 10 NA Asia South-Eastern Asia
## 11 3.162743e+12 Americas Northern America
## 12 NA Asia South-Eastern Asia
## 13 3.368321e+12 Americas Northern America
## 14 NA Asia South-Eastern Asia
## 15 3.452529e+12 Americas Northern America
## 16 NA Asia South-Eastern Asia
## 17 3.618250e+12 Americas Northern America
## 18 NA Asia South-Eastern Asia
## 19 3.730416e+12 Americas Northern America
## 20 NA Asia South-Eastern Asia
## 21 3.737877e+12 Americas Northern America
## 22 NA Asia South-Eastern Asia
## 23 3.867133e+12 Americas Northern America
## 24 NA Asia South-Eastern Asia
## 25 4.080668e+12 Americas Northern America
## 26 NA Asia South-Eastern Asia
## 27 4.321881e+12 Americas Northern America
## 28 NA Asia South-Eastern Asia
## 29 4.299437e+12 Americas Northern America
## 30 NA Asia South-Eastern Asia
## 31 4.291009e+12 Americas Northern America
## 32 NA Asia South-Eastern Asia
## 33 4.523528e+12 Americas Northern America
## 34 NA Asia South-Eastern Asia
## 35 4.733337e+12 Americas Northern America
## 36 NA Asia South-Eastern Asia
## 37 4.999656e+12 Americas Northern America
## 38 NA Asia South-Eastern Asia
## 39 5.157035e+12 Americas Northern America
## 40 NA Asia South-Eastern Asia
## 41 5.142220e+12 Americas Northern America
## 42 NA Asia South-Eastern Asia
## 43 5.272896e+12 Americas Northern America
## 44 NA Asia South-Eastern Asia
## 45 5.168479e+12 Americas Northern America
## 46 NA Asia South-Eastern Asia
## 47 5.401886e+12 Americas Northern America
## 48 NA Asia South-Eastern Asia
## 49 5.790542e+12 Americas Northern America
## 50 1.145347e+10 Asia South-Eastern Asia
## 51 6.028651e+12 Americas Northern America
## 52 1.188938e+10 Asia South-Eastern Asia
## 53 6.235265e+12 Americas Northern America
## 54 1.222101e+10 Asia South-Eastern Asia
## 55 6.432743e+12 Americas Northern America
## 56 1.265894e+10 Asia South-Eastern Asia
## 57 6.696490e+12 Americas Northern America
## 58 1.330898e+10 Asia South-Eastern Asia
## 59 6.935219e+12 Americas Northern America
## 60 1.428912e+10 Asia South-Eastern Asia
## 61 7.063943e+12 Americas Northern America
## 62 1.501800e+10 Asia South-Eastern Asia
## 63 7.045491e+12 Americas Northern America
## 64 1.591320e+10 Asia South-Eastern Asia
## 65 7.285373e+12 Americas Northern America
## 66 1.728906e+10 Asia South-Eastern Asia
## 67 7.494650e+12 Americas Northern America
## 68 1.868476e+10 Asia South-Eastern Asia
## 69 7.803020e+12 Americas Northern America
## 70 2.033630e+10 Asia South-Eastern Asia
## 71 8.001917e+12 Americas Northern America
## 72 2.227648e+10 Asia South-Eastern Asia
## 73 8.304875e+12 Americas Northern America
## 74 2.435711e+10 Asia South-Eastern Asia
## 75 8.679071e+12 Americas Northern America
## 76 2.634272e+10 Asia South-Eastern Asia
## 77 9.061073e+12 Americas Northern America
## 78 2.786124e+10 Asia South-Eastern Asia
## 79 9.502248e+12 Americas Northern America
## 80 2.919122e+10 Asia South-Eastern Asia
## 81 9.898800e+12 Americas Northern America
## 82 3.117252e+10 Asia South-Eastern Asia
## 83 1.000703e+13 Americas Northern America
## 84 3.332183e+10 Asia South-Eastern Asia
## 85 1.018996e+13 Americas Northern America
## 86 3.568108e+10 Asia South-Eastern Asia
## 87 1.045007e+13 Americas Northern America
## 88 3.830049e+10 Asia South-Eastern Asia
## 89 1.081371e+13 Americas Northern America
## 90 4.128394e+10 Asia South-Eastern Asia
## 91 1.114630e+13 Americas Northern America
## 92 4.476905e+10 Asia South-Eastern Asia
## 93 1.144269e+13 Americas Northern America
## 94 4.845303e+10 Asia South-Eastern Asia
## 95 1.166093e+13 Americas Northern America
## 96 5.255039e+10 Asia South-Eastern Asia
## 97 1.161905e+13 Americas Northern America
## 98 5.586668e+10 Asia South-Eastern Asia
## 99 1.120919e+13 Americas Northern America
## 100 5.884079e+10 Asia South-Eastern Asia
## 101 1.154791e+13 Americas Northern America
## 102 6.283222e+10 Asia South-Eastern Asia
Now that you have created the data table in Exercise 4, it is time to plot the data for the two countries.
Use geom_line to plot life expectancy vs year for Vietnam and the United States and save the plot as p. The data table is stored in tab.
Use color to distinguish the two countries.
Print the object p.
p <- tab %>%
ggplot(aes(year, life_expectancy, color = country)) +
geom_line()
p
Cambodia was also involved in this conflict and, after the war, Pol Pot and his communist Khmer Rouge took control and ruled Cambodia from 1975 to 1979. He is considered one of the most brutal dictators in history. Do the data support this claim?
Use a single line of code to create a time series plot from 1960 to 2010 of life expectancy vs year for Cambodia.
p <- gapminder %>%
filter(country == "Cambodia", year >= 1960 & year <= 2010) %>%
ggplot(aes(year, life_expectancy)) +
geom_line()
p
Now we are going to calculate and plot dollars per day for African countries in 2010 using GDP data.
In the first part of this analysis, we will create the dollars per day variable.
Use mutate to create a dollars_per_day variable, which is defined as gdp/population/365.
Create the dollars_per_day variable for African countries for the year 2010.
Remove any NA values.
Save the mutated dataset as daydollars.
library(dplyr)
library(dslabs)
data(gapminder)
daydollars <- gapminder %>%
mutate(dollars_per_day = gdp/population/365) %>%
filter(continent == "Africa", year == 2010, !is.na(gdp))
daydollars
## country year infant_mortality life_expectancy fertility
## 1 Algeria 2010 23.5 76.0 2.82
## 2 Angola 2010 109.6 57.6 6.22
## 3 Benin 2010 71.0 60.8 5.10
## 4 Botswana 2010 39.8 55.6 2.76
## 5 Burkina Faso 2010 69.7 59.0 5.87
## 6 Burundi 2010 63.8 60.4 6.30
## 7 Cameroon 2010 66.2 57.8 5.02
## 8 Cape Verde 2010 23.3 71.1 2.43
## 9 Central African Republic 2010 101.7 47.9 4.63
## 10 Chad 2010 93.6 55.8 6.60
## 11 Comoros 2010 63.1 67.7 4.92
## 12 Congo, Dem. Rep. 2010 84.8 58.4 6.25
## 13 Congo, Rep. 2010 42.2 60.4 5.07
## 14 Cote d'Ivoire 2010 76.9 56.6 4.91
## 15 Egypt 2010 24.3 70.1 2.88
## 16 Equatorial Guinea 2010 78.9 58.6 5.14
## 17 Eritrea 2010 39.4 60.1 4.97
## 18 Ethiopia 2010 50.8 62.1 4.90
## 19 Gabon 2010 42.8 63.0 4.21
## 20 Gambia 2010 51.7 66.5 5.80
## 21 Ghana 2010 50.2 62.9 4.05
## 22 Guinea 2010 71.2 57.9 5.17
## 23 Guinea-Bissau 2010 73.4 54.3 5.12
## 24 Kenya 2010 42.4 62.9 4.62
## 25 Lesotho 2010 75.2 46.4 3.21
## 26 Liberia 2010 65.2 60.8 5.02
## 27 Madagascar 2010 42.1 62.4 4.65
## 28 Malawi 2010 57.5 55.4 5.64
## 29 Mali 2010 82.9 59.2 6.84
## 30 Mauritania 2010 70.1 68.6 4.84
## 31 Mauritius 2010 13.3 73.4 1.52
## 32 Morocco 2010 28.5 73.7 2.58
## 33 Mozambique 2010 71.9 54.4 5.41
## 34 Namibia 2010 37.5 61.4 3.23
## 35 Niger 2010 66.1 59.2 7.58
## 36 Nigeria 2010 81.5 61.2 6.02
## 37 Rwanda 2010 43.8 65.1 4.84
## 38 Senegal 2010 46.7 64.2 5.05
## 39 Seychelles 2010 12.2 73.1 2.26
## 40 Sierra Leone 2010 107.0 55.0 4.94
## 41 South Africa 2010 38.2 54.9 2.47
## 42 Sudan 2010 53.3 66.1 4.64
## 43 Swaziland 2010 59.1 46.4 3.56
## 44 Tanzania 2010 42.4 61.4 5.43
## 45 Togo 2010 59.3 58.7 4.79
## 46 Tunisia 2010 14.9 77.1 2.04
## 47 Uganda 2010 49.5 57.8 6.16
## 48 Zambia 2010 52.9 53.1 5.81
## 49 Zimbabwe 2010 55.8 49.1 3.72
## population gdp continent region dollars_per_day
## 1 36036159 79164339611 Africa Northern Africa 6.0186382
## 2 21219954 26125663270 Africa Middle Africa 3.3731063
## 3 9509798 3336801340 Africa Western Africa 0.9613161
## 4 2047831 8408166868 Africa Southern Africa 11.2490111
## 5 15632066 4655655008 Africa Western Africa 0.8159650
## 6 9461117 1158914103 Africa Eastern Africa 0.3355954
## 7 20590666 13986616694 Africa Middle Africa 1.8610130
## 8 490379 971606715 Africa Western Africa 5.4283242
## 9 4444973 1054122016 Africa Middle Africa 0.6497240
## 10 11896380 3369354207 Africa Middle Africa 0.7759594
## 11 698695 247231031 Africa Eastern Africa 0.9694434
## 12 65938712 6961485000 Africa Middle Africa 0.2892468
## 13 4066078 5067059617 Africa Middle Africa 3.4141881
## 14 20131707 11603002049 Africa Western Africa 1.5790537
## 15 82040994 160258746162 Africa Northern Africa 5.3517764
## 16 728710 5979285835 Africa Middle Africa 22.4802803
## 17 4689664 771116883 Africa Eastern Africa 0.4504905
## 18 87561814 18291486355 Africa Eastern Africa 0.5723232
## 19 1541936 6343809583 Africa Middle Africa 11.2717391
## 20 1693002 1217357172 Africa Western Africa 1.9700066
## 21 24317734 8779397392 Africa Western Africa 0.9891194
## 22 11012406 5493989673 Africa Western Africa 1.3668245
## 23 1634196 244395463 Africa Western Africa 0.4097285
## 24 40328313 18988282813 Africa Eastern Africa 1.2899794
## 25 2010586 1076239050 Africa Southern Africa 1.4665377
## 26 3957990 1040653199 Africa Western Africa 0.7203416
## 27 21079532 5026822443 Africa Eastern Africa 0.6533407
## 28 14769824 2758392725 Africa Eastern Africa 0.5116676
## 29 15167286 4199858651 Africa Western Africa 0.7586368
## 30 3591400 2107593972 Africa Western Africa 1.6077936
## 31 1247951 6636426093 Africa Eastern Africa 14.5694737
## 32 32107739 59908047776 Africa Northern Africa 5.1119027
## 33 24321457 8972305823 Africa Eastern Africa 1.0106985
## 34 2193643 6155469329 Africa Southern Africa 7.6878050
## 35 16291990 2781188119 Africa Western Africa 0.4676957
## 36 159424742 85581744176 Africa Western Africa 1.4707286
## 37 10293669 3583713093 Africa Eastern Africa 0.9538282
## 38 12956791 6984284544 Africa Western Africa 1.4768337
## 39 93081 760361490 Africa Eastern Africa 22.3803157
## 40 5775902 1574302614 Africa Western Africa 0.7467505
## 41 51621594 187639624489 Africa Southern Africa 9.9586457
## 42 36114885 22819076998 Africa Northern Africa 1.7310873
## 43 1193148 1911603442 Africa Southern Africa 4.3894552
## 44 45648525 19965679449 Africa Eastern Africa 1.1982970
## 45 6390851 1595792895 Africa Western Africa 0.6841085
## 46 10639194 33161453137 Africa Northern Africa 8.5394905
## 47 33149417 12701095116 Africa Eastern Africa 1.0497174
## 48 13917439 5587389858 Africa Eastern Africa 1.0999091
## 49 13973897 4032423429 Africa Eastern Africa 0.7905980
Now we are going to calculate and plot dollars per day for African countries in 2010 using GDP data.
In the second part of this analysis, we will plot the smooth density plot using a log (base 2) x axis.
The dataset including the dollars_per_day variable is preloaded as daydollars. Create a smooth density plot of dollars per day from daydollars.
Use scale_x_continuous to change the x-axis to a log (base 2) scale.
p <- daydollars %>%
ggplot(aes(dollars_per_day, y = ..count..)) +
geom_density() +
scale_x_continuous(trans = "log2")
p
Now we are going to combine the plotting tools we have used in the past two exercises to create density plots for multiple years.
Create the dollars_per_day variable as in Exercise 7, but for African countries in the years 1970 and 2010 this time. Make sure you remove any NA values.
Create a smooth density plot of dollars per day for 1970 and 2010 using a log (base 2) scale for the x axis.
Use facet_grid to show a different density plot for 1970 and 2010.
library(dplyr)
library(ggplot2)
library(dslabs)
data(gapminder)
daydollars <- gapminder %>%
filter(continent == "Africa", year %in% c(1970, 2010), !is.na(gdp)) %>%
mutate(dollars_per_day = gdp/population/365)
p <- daydollars %>%
ggplot(aes(dollars_per_day, y = ..count..)) +
geom_density() +
scale_x_continuous(trans = "log2") +
facet_grid(. ~ year)
p
Now we are going to edit the code from Exercise 9 to show a stacked density plot of each region in Africa.
Much of the code will be the same as in Exercise 9:
Create the dollars_per_day variable as in Exercise 7, but for African countries in the years 1970 and 2010 this time.
Make sure you remove any NA values.
Create a smooth density plot of dollars per day for 1970 and 2010 using a log (base 2) scale for the x axis.
Use facet_grid to show a different density plot for 1970 and 2010.
Make sure the densities are smooth by using bw = 0.5.
Use the fill and position arguments where appropriate to create the stacked density plot of each region.
library(dplyr)
library(ggplot2)
library(dslabs)
data(gapminder)
daydollars <- gapminder %>%
filter(continent == "Africa", year %in% c(1970, 2010), !is.na(gdp)) %>%
mutate(dollars_per_day = gdp/population/365)
p <- daydollars %>%
ggplot(aes(dollars_per_day, y = ..count.., fill=region)) +
geom_density(bw = 0.5, position = "stack") +
scale_x_continuous(trans = "log2") +
facet_grid(year ~ .)
p
We are going to continue looking at patterns in the gapminder dataset by plotting infant mortality rates versus dollars per day for African countries.
Generate dollars_per_day using mutate and filter for the year 2010 for African countries. Remember to remove NA values.
Store the mutated dataset in gapminder_Africa_2010.
Make a scatter plot of infant_mortality versus dollars_per_day for countries in the African continent.
Use color to denote the different regions of Africa.
library(dplyr)
library(ggplot2)
library(dslabs)
data(gapminder)
gapminder_Africa_2010 <- # create the mutated dataset
gapminder %>% filter(continent == "Africa", year == 2010, !is.na(gdp)) %>%
mutate(dollars_per_day = gdp/population/365)
# now make the scatter plot
p <- gapminder_Africa_2010 %>%
ggplot(aes(dollars_per_day, infant_mortality, color = region)) +
geom_point()
p
Now we are going to transform the x axis of the plot from the previous exercise.
The mutated dataset is preloaded as gapminder_Africa_2010.
As in the previous exercise, make a scatter plot of infant_mortality versus dollars_per_day for countries in the African continent.
As in the previous exercise, use color to denote the different regions of Africa.
Transform the x axis to be in the log (base 2) scale.
gapminder_Africa_2010 %>% # your plotting code here
ggplot(aes(dollars_per_day, infant_mortality, color = region)) +
geom_point() +
scale_x_continuous(trans = "log2")
Note that there is a large variation in infant mortality and dollars per day among African countries.
As an example, one country has infant mortality rates of less than 20 per 1000 and dollars per day of 16, while another country has infant mortality rates over 10% and dollars per day of about 1.
In this exercise, we will remake the plot from Exercise 12 with country names instead of points so we can identify which countries are which.
The mutated dataset is preloaded as gapminder_Africa_2010.
As in the previous exercise, make a scatter plot of infant_mortality versus dollars_per_day for countries in the African continent.
As in the previous exercise, use color to denote the different regions of Africa.
As in the previous exercise, transform the x axis to be in the log (base 2) scale.
Add a geom_text layer to display country names in addition to of points.
gapminder_Africa_2010 %>% # your plotting code here
ggplot(aes(dollars_per_day, infant_mortality, color = region, label = country)) +
geom_point() +
scale_x_continuous(trans = "log2") +
geom_text()
Now we are going to look at changes in the infant mortality and dollars per day patterns African countries between 1970 and 2010.
Generate dollars_per_day using mutate and filter for the years 1970 and 2010 for African countries. Remember to remove NA values.
As in the previous exercise, make a scatter plot of infant_mortality versus dollars_per_day for countries in the African continent.
As in the previous exercise, use color to denote the different regions of Africa.
As in the previous exercise, transform the x axis to be in the log (base 2) scale.
As in the previous exercise, add a layer to display country names instead of points.
Use facet_grid to show different plots for 1970 and 2010. Align the plots vertically.
library(dplyr)
library(ggplot2)
library(dslabs)
data(gapminder)
daydollars <- gapminder %>%
filter(continent == "Africa", year %in% c(1970, 2010), !is.na(gdp), !is.na(infant_mortality)) %>%
mutate(dollars_per_day = gdp/population/365)
p <- daydollars %>%
ggplot(aes(dollars_per_day, infant_mortality, color = region, label = country)) +
geom_point() +
scale_x_continuous(trans = "log2") +
geom_text()+
facet_grid(year ~ .)
p