1 load libraries

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)

2 load data

gapminder <- read.csv("C:\\Users\\Sickdoctor\\Downloads\\gapminder_data.csv")
View(gapminder)

Q4: Scatter plot - lifeExp vs gdpPercap

ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

Q5: Colour by continent

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()

Q7: Facet by continent

ggplot(gapminder, aes(x = log10(gdpPercap), y = lifeExp, colour = continent)) +
  geom_point() +
  facet_grid(~ continent)

Q8: Add theme, save as plot1

plot1 <- ggplot(gapminder, aes(x = log10(gdpPercap), y = lifeExp, colour = continent)) +
  geom_point() +
  facet_grid(~ continent) +
  theme_bw()
plot1

Q9: Density plot of lifeExp by continent

ggplot(gapminder, aes(x = lifeExp, fill = continent)) +
  geom_density(alpha = 0.5)

Q10: lifeExp vs year, line plot by country, save as plot2

plot2 <- ggplot(gapminder, aes(x = year, y = lifeExp, colour = country)) +
  geom_line() +
  theme(legend.position = "none")
plot2

Q11: gapminder_2007

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.

Q12: Point plot continent vs mean GDP per capita

ggplot(gapminder_2007, aes(x = continent, y = mean_gdpPercap, colour = continent)) +
  geom_point()

Q13: Bar plot with labels and theme

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()

Q14: gapminder_summary

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

Q15: Line + point plot, save as plot3

plot3 <- ggplot(gapminder_summary, aes(x = year, y = mean_lifeExp, colour = continent)) +
  geom_line() +
  geom_point()
plot3

Q16: kenya_gapminder

kenya_gapminder <- gapminder %>%
  filter(country == "Kenya")

Q17: Year vs GDP per capita for 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()

Q18: gapminder_decline

gapminder_decline <- gapminder %>%
  filter(year %in% c(1997, 2002, 2007), diff_lifeExp < 0)

Q19: Bar plot faceted by year

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))

Q20: Colour by country, remove legend, labels, theme, save as plot4

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) ```