nations <- read_csv("nations.csv")
glimpse(nations)
## Rows: 5,275
## Columns: 10
## $ iso2c <chr> "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", ...
## $ iso3c <chr> "AND", "AND", "AND", "AND", "AND", "AND", "AND",...
## $ country <chr> "Andorra", "Andorra", "Andorra", "Andorra", "And...
## $ year <dbl> 1996, 1994, 2003, 1990, 2009, 2011, 2004, 2010, ...
## $ gdp_percap <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ population <dbl> 64291, 62707, 74783, 54511, 85474, 82326, 78337,...
## $ birth_rate <dbl> 10.900, 10.900, 10.300, 11.900, 9.900, NA, 10.90...
## $ neonat_mortal_rate <dbl> 2.8, 3.2, 2.0, 4.3, 1.7, 1.6, 2.0, 1.7, 2.1, 2.1...
## $ region <chr> "Europe & Central Asia", "Europe & Central Asia"...
## $ income <chr> "High income", "High income", "High income", "Hi...
Add a column for the GDP in trillions of dollars of each country.
nations <- nations %>%
mutate(gdp = gdp_percap * population / 10^12)
glimpse(nations)
## Rows: 5,275
## Columns: 11
## $ iso2c <chr> "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", ...
## $ iso3c <chr> "AND", "AND", "AND", "AND", "AND", "AND", "AND",...
## $ country <chr> "Andorra", "Andorra", "Andorra", "Andorra", "And...
## $ year <dbl> 1996, 1994, 2003, 1990, 2009, 2011, 2004, 2010, ...
## $ gdp_percap <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ population <dbl> 64291, 62707, 74783, 54511, 85474, 82326, 78337,...
## $ birth_rate <dbl> 10.900, 10.900, 10.300, 11.900, 9.900, NA, 10.90...
## $ neonat_mortal_rate <dbl> 2.8, 3.2, 2.0, 4.3, 1.7, 1.6, 2.0, 1.7, 2.1, 2.1...
## $ region <chr> "Europe & Central Asia", "Europe & Central Asia"...
## $ income <chr> "High income", "High income", "High income", "Hi...
## $ gdp <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
Choose four countries and then sketch the line plots of their GDPs over years.
big4 <- nations %>%
filter(iso3c == "CHN" | iso3c == "DEU" | iso3c == "JPN" | iso3c == "USA")
levels(factor(big4$country))
## [1] "China" "Germany" "Japan" "United States"
gg1 <- ggplot(big4, aes(year, gdp, color = country)) +
geom_point() +
geom_line() +
scale_color_brewer(palette = "Set1") +
labs(title = "GDPs of Big 4 Counties",
x = "Year",
y = "GDP (trillions of dollars)") +
theme(plot.title = element_text(face = "bold", hjust = 0.5),
legend.title = element_blank()) # remove title for the below
ggplotly(gg1) %>%
add_annotations(text = "Country", xref = "paper", yref = "paper",
x = 1.047, xanchor = "left",
y = 0.7, yanchor = "bottom", # Same y as legend below
legendtitle = TRUE, showarrow = FALSE) %>% # legend title
layout(legend = list(y = 0.7, yanchor = "top")) # legend
Regroup the data nations by year and region. Compute the total GDP of the world categorized by regions in each year and then plot them with areas over years.
by_region <- nations %>%
group_by(year, region) %>%
summarise(gdp_sum = sum(gdp, na.rm = TRUE))
gg2 <- ggplot(by_region) +
geom_area(aes(year, gdp_sum, fill = region), color = "white") +
scale_fill_brewer(palette = "Set2") +
labs(title = "Total GDPs of the World",
x = "Year",
y = "Total GDP (trillions of dollars)") +
theme(plot.title = element_text(face = "bold", hjust = 0.5),
legend.title = element_blank())
ggplotly(gg2) %>%
add_annotations(text = "Region", xref = "paper", yref = "paper",
x = 1.03, xanchor = "left",
y = 0.8, yanchor = "bottom",
legendtitle = TRUE, showarrow = FALSE) %>%
layout(legend = list(y = 0.8, yanchor = "top"))