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
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats 1.0.0 ✔ readr 2.1.5
✔ ggplot2 3.5.2 ✔ stringr 1.5.1
✔ lubridate 1.9.4 ✔ tibble 3.2.1
✔ purrr 1.0.4 ✔ tidyr 1.3.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
setwd ("~/Downloads" )
nations <- read_csv ("~/Downloads/nations.csv" )
Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
nationsnona <- nations |>
filter (! is.na (gdp_percap) & ! is.na (population))
nations1 <- nationsnona |>
mutate (TrillionGDP = (gdp_percap * population) / 1000000000000 )
nations2 <- nations1 |>
filter (country %in% c ("United States" , "China" , "Japan" , "Germany" ))
p1 <- ggplot (nations2, aes (x = year, y = TrillionGDP, color = country)) +
geom_line (size = 0.8 ) +
geom_point (size = 2 ) +
labs (
title = "China's Rise to Become the Largest Economy" ,
x = "year" ,
y = "GDP (Trillions $)" ,
color = "Country" ,
) +
scale_x_continuous (breaks = seq (1990 ,2015 ,5 ), limits = c (1990 , 2015 )) +
scale_y_continuous (breaks = seq (0 ,15 ,5 ), limits = c (0 , 18 )) +
scale_color_brewer (palette = "Set1" )
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_line()`).
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_point()`).
regional_gdp <- nations1 |>
group_by (region, year) |>
summarise (
TRILLIONGDP = sum (TrillionGDP, na.rm = TRUE ),
)
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
p2 <- ggplot (regional_gdp, aes (x = year, y = TRILLIONGDP, fill = region)) +
geom_area (color = "white" , size = 0.2 ) +
labs (
title = "GDP by World Bank Region" ,
x = "year" ,
y = "GDP (Trillions $)" ,
fill = "region"
) +
scale_x_continuous (
breaks = seq (1990 , 2015 , 5 ),
limits = c (1990 , 2015 )
) +
scale_y_continuous (
breaks = seq (0 , 100 , 25 ),
limits = c (0 , 105 ) +
scale_color_brewer (palette = "Set2" ) #didnt change the color?
)
p2
scale_fill_manual(c(
“East Asia & Pacific” = “#66C2A5”,
“Europe & Central Asia” = “#FC8D62”,
“Latin America & Caribbean” = “#8DA0CB”, “Middle East & North Africa” = “#E78AC3”, “North America” = “#A6D854”,
“South Asia” = “#FFD92F”,
“Sub-Saharan Africa” = “#E5C494”))