# load data
nations <- read.csv("~/DATA 110/nations.csv")Yan two Graphs
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
# Adding a new variable
nations <- nations |>
mutate(nations, GDP_trillions = (gdp_percap * population)/1e+12, na.rm = TRUE)
# View the added variable
View(nations)library(dplyr)
# filter the data to 4 countries
filtered_nations <- nations %>%
filter(country %in% c("Belgium", "Chile", "Colombia", "Cuba"))
# View the filtered data
View(filtered_nations)library(ggplot2)
library(RColorBrewer)
# Set 1 from RColorBrewer
colors <- brewer.pal(4, "Set1")
# Create the dot line plot
ggplot(filtered_nations, aes(x = year, y = GDP_trillions, color = country)) +
geom_line() +
geom_point() +
scale_color_manual(values = colors) +
labs(title = "GDP Over Time for 4 Countries",
x = "Year",
y = "GDP (in Trillions of Dollars)") +
theme_minimal()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()`).
library(dplyr)
# Adding a new variable
nations <- nations %>%
mutate(GDP_trillions = (gdp_percap * population) / 1e12)
# View the added variable
View(nations)library(dplyr)
# Group by region and year, then summarize the total GDP
summarized_data <- nations %>%
group_by(region, year) %>%
summarise(GDP = sum(GDP_trillions, na.rm = TRUE))`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
# View the summarized data
View(summarized_data)library(ggplot2)
library(RColorBrewer)
# summary of the 7 regions
summarized_data# A tibble: 175 × 3
# Groups: region [7]
region year GDP
<chr> <int> <dbl>
1 East Asia & Pacific 1990 5.52
2 East Asia & Pacific 1991 6.03
3 East Asia & Pacific 1992 6.50
4 East Asia & Pacific 1993 7.04
5 East Asia & Pacific 1994 7.64
6 East Asia & Pacific 1995 8.29
7 East Asia & Pacific 1996 8.96
8 East Asia & Pacific 1997 9.55
9 East Asia & Pacific 1998 9.60
10 East Asia & Pacific 1999 10.1
# ℹ 165 more rows
region <- c("East Asia & Pacific", "Europe & Central Asia", "Latin America & Caribbean", "Middle East & North Africa", "North America", "South Asia", "Sub-Saharan Africa")
# Set color palette
colors <- brewer.pal(7, "Set2")
# Create an area plot with thin white lines
ggplot(summarized_data, aes(x = year, y = GDP, fill = region)) +
geom_area(color = "white", linewidth = 0.2) +
scale_fill_manual(values = colors) +
labs(title = "GDP Over Time for 7 Regions",
x = "Year",
y = "GDP (Trillions)") +
theme_minimal()