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)
gapminder <- read.csv("C:\\Users\\Sickdoctor\\Downloads\\gapminder_data.csv")
View(gapminder)
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent)) +
geom_point()
## Q6: Log10 transform (x-axis, since gdpPercap is on x here)
ggplot(gapminder, aes(x = log10(gdpPercap), y = lifeExp, colour = continent)) +
geom_point()
ggplot(gapminder, aes(x = log10(gdpPercap), y = lifeExp, colour = continent)) +
geom_point() +
facet_grid(~ continent)
plot1 <- ggplot(gapminder, aes(x = log10(gdpPercap), y = lifeExp, colour = continent)) +
geom_point() +
facet_grid(~ continent) +
theme_bw()
plot1
ggplot(gapminder, aes(x = lifeExp, fill = continent)) +
geom_density(alpha = 0.5)
plot2 <- ggplot(gapminder, aes(x = year, y = lifeExp, colour = country)) +
geom_line() +
theme(legend.position = "none")
plot2
gapminder_2007 <- gapminder %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarise(mean_gdpPercap = mean(gdpPercap))
gapminder_2007
## # A tibble: 5 × 2
## continent mean_gdpPercap
## <chr> <dbl>
## 1 Africa 3089.
## 2 Americas 11003.
## 3 Asia 12473.
## 4 Europe 25054.
## 5 Oceania 29810.
ggplot(gapminder_2007, aes(x = continent, y = mean_gdpPercap, colour = continent)) +
geom_point()
ggplot(gapminder_2007, aes(x = continent, y = mean_gdpPercap, fill = continent)) +
geom_col() +
labs(x = "Continent", y = "Mean GDP per Capita",
title = "Mean GDP per Capita by Continent (2007)") +
theme_minimal()
gapminder_summary <- gapminder %>%
group_by(continent, year) %>%
summarise(mean_gdpPercap = mean(gdpPercap),
mean_lifeExp = mean(lifeExp),
.groups = "drop")
gapminder_summary
## # A tibble: 60 × 4
## continent year mean_gdpPercap mean_lifeExp
## <chr> <int> <dbl> <dbl>
## 1 Africa 1952 1253. 39.1
## 2 Africa 1957 1385. 41.3
## 3 Africa 1962 1598. 43.3
## 4 Africa 1967 2050. 45.3
## 5 Africa 1972 2340. 47.5
## 6 Africa 1977 2586. 49.6
## 7 Africa 1982 2482. 51.6
## 8 Africa 1987 2283. 53.3
## 9 Africa 1992 2282. 53.6
## 10 Africa 1997 2379. 53.6
## # ℹ 50 more rows
plot3 <- ggplot(gapminder_summary, aes(x = year, y = mean_lifeExp, colour = continent)) +
geom_line() +
geom_point()
plot3
kenya_gapminder <- gapminder %>%
filter(country == "Kenya")
ggplot(kenya_gapminder, aes(x = year, y = gdpPercap)) +
geom_line(colour = "blue") +
geom_point(colour = "blue") +
labs(x = "Year", y = "GDP per Capita",
title = "Kenya: GDP per Capita over Time") +
theme_minimal()
gapminder_decline <- gapminder %>%
filter(year %in% c(1997, 2002, 2007), diff_lifeExp < 0)
ggplot(gapminder_decline, aes(x = country, y = diff_lifeExp)) +
geom_col() +
facet_grid(~ year, scales = "free_x") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
plot4 <- ggplot(gapminder_decline, aes(x = country, y = diff_lifeExp, fill = country)) +
geom_col() +
facet_grid(~ year, scales = "free_x") +
labs(x = "Country", y = "Difference in Life Expectancy",
title = "Countries with Declining Life Expectancy") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
legend.position = "none")
plot4
t1 + plot2) / (plot3 + plot4) ```