Modify ggplot

Sample Dataset

Exercises

Task:

  • Filter the year 2007 from the Gapminder dataset.

  • Create a scatter plot of GDP per capita vs Life Expectancy.

  • Color the points by continent.

  • Apply custom theme settings:

    • Title: Bold, size 20, color darkblue.

    • X & Y labels: Italic, size 14, color red.

    • Axis text: Size 12, color blue.

    • Legend title: Size 14, color black.

    • Legend text: Size 12, color darkgreen.

    • Remove grid lines.

Solution:

gapminder_2007 |> 

ggplot() +
  aes(x = gdpPercap, y = lifeExp, color = continent) +
  geom_point(size = 3) +
  labs(title = "GDP per Capita vs Life Expectancy (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme(
    plot.title = element_text(size = 20, face = "bold", color = "darkblue"),
    axis.title.x = element_text(size = 14, face = "italic", color = "red"),
    axis.title.y = element_text(size = 14, face = "italic", color = "red"),
    axis.text = element_text(size = 12, color = "blue"),
    legend.title = element_text(size = 14, color = "black"),
    legend.text = element_text(size = 12, color = "darkgreen"),
    panel.grid = element_blank()
  )

Task:

  • Filter year 2007 from Gapminder.

  • Identify the top 5 richest countries (highest GDP per capita).

  • Identify the top 5 poorest countries (lowest GDP per capita).

  • Add annotations:

    • Richest (blue)

    • Poorest (red)

  • Rotate X-axis labels 30 degrees.

Solution:

gapminder_2007 <- gapminder |> 
  filter(year == 2007)

top5 <- gapminder_2007 |> 
  arrange(desc(gdpPercap)) |> 
  head(5)

bottom5 <- gapminder_2007 |> 
  arrange(gdpPercap) |> 
  head(5)

gapminder_2007 |> 

ggplot() +
  aes(x = gdpPercap, y = lifeExp)+
  geom_point(aes(color = continent), size = 3) +
  geom_text(data = top5, aes(label = country), vjust = -1, color = "blue", size = 4) +
  geom_text(data = bottom5, aes(label = country), vjust = 1, color = "red", size = 4) +
  labs(title = "GDP per Capita vs Life Expectancy (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme(
    plot.title = element_text(size = 18, face = "bold", color = "darkblue"),
    axis.text.x = element_text(angle = 30, hjust = 1)
  )

Task:

  • Filter year 2007 from Gapminder.

  • Color all continents gray except Asia (red).

  • Add an annotation box for explanation.

Solution:

gapminder_2007 |> 
ggplot() +
  aes(x = gdpPercap, y = lifeExp) +
  geom_point(aes(color = ifelse(continent == "Asia", "Asia", "Other")), size = 3) +
  scale_color_manual(values = c("Asia" = "red", "Other" = "gray")) +
  annotate(geom = "text", x = 50000, y = 40, label = "Asia is highlighted", color = "red", size = 5, fontface = "bold") +
  labs(title = "GDP per Capita vs Life Expectancy (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme(legend.position = "none")

Task:

  • Filter year 2007 from Gapminder.

  • Shade a horizontal region where life expectancy is between 60 and 70 years using a light blue background.

Solution:

gapminder_2007 |> 
ggplot() +
aes(x = gdpPercap, y = lifeExp) +
  geom_rect(aes(xmin = min(gdpPercap), xmax = max(gdpPercap), ymin = 60, ymax = 70), 
            fill = "lightblue", alpha = 0.2) +
  geom_point(aes(color = continent), size = 3) +
  labs(title = "GDP per Capita vs Life Expectancy (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme_minimal()

Task:

  • Filter year 2007 from Gapminder.

  • Create facet plot of GDP per capita vs Life Expectancy for each continent.

  • Customize:

    • Panel background: Light gray.

    • Title text: Bold.

    • Facet strip background: Dark blue with white text.

Solution:

gapminder_2007 |> 
ggplot() +
  aes(x = gdpPercap, y = lifeExp) +
  geom_point(aes(color = continent), size = 3) +
  facet_wrap(~continent) +
  labs(title = "GDP per Capita vs Life Expectancy by Continent (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme(
    plot.title = element_text(size = 18, face = "bold"),
    panel.background = element_rect(fill = "lightgray"),
    strip.background = element_rect(fill = "darkblue"),
    strip.text = element_text(color = "white", face = "bold")
  )

Task:

  • Filter year 2007 from Gapminder.

  • Identify the top 5 most populous countries.

  • Increase the size of these points and label them.

  • Adjust the theme:

    • Title: Bold, blue.

    • X & Y labels: Gray, italic.

    • Legend position: Bottom.

Solution:



top5_pop <- gapminder_2007 |>
  arrange(desc(pop)) |>
  head(5)

gapminder_2007 |> 
ggplot() +
  aes(x = gdpPercap, y = lifeExp, size = pop, color = continent) +
  geom_point(alpha = 0.6) +
  geom_point(data = top5_pop, aes(size = pop), color = "black", shape = 21, stroke = 2) +
  geom_text(data = top5_pop, aes(label = country), vjust = -1, size = 4) +
  labs(title = "Emphasizing the Most Populous Countries",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme(
    plot.title = element_text(size = 18, face = "bold", color = "blue"),
    axis.title = element_text(size = 14, face = "italic", color = "gray"),
    legend.position = "bottom"
  )

Task:

  • Filter year 2007 from Gapminder.

  • Create a scatter plot of GDP per capita vs Life Expectancy.

  • Add a LOESS trend line.

  • Annotate the general trend.

Solution:

gapminder_2007 |> 
ggplot() +
  aes(x = gdpPercap, y = lifeExp) +
  geom_point(aes(color = continent), size = 3) +
  geom_smooth(method = "loess", se = FALSE, color = "black", linetype = "dashed") +
  annotate(geom = "text", x = 50000, y = 45, label = "Higher GDP tends to increase Life Expectancy", color = "red", size = 5) +
  labs(title = "GDP per Capita vs Life Expectancy (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme_minimal()

Task:

  • Filter year 2007 from Gapminder.

  • Create a facet grid by continent.

  • Customize the facet titles:

    • Strip background: Dark blue.

    • Strip text: White, bold.

Solution:

gapminder_2007 |> 
ggplot() +
  aes(x = gdpPercap, y = lifeExp, color = continent) +
  geom_point(size = 3) +
  facet_grid(.~continent) +
  labs(title = "Life Expectancy by Continent (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme(
    strip.background = element_rect(fill = "darkblue"),
    strip.text = element_text(color = "white", face = "bold"),
    plot.title = element_text(size = 18, face = "bold")
  )

Task:

  • Filter year 2007 from Gapminder.

  • Highlight Thailand (make it red, while others are gray).

  • Add a label for Thailand.

Solution:

gapminder_2007 |> 
ggplot() +
 aes(x = gdpPercap, y = lifeExp) +
  geom_point(aes(color = ifelse(country == "Thailand", "Thailand", "Other")), size = 3) +
  scale_color_manual(values = c("Thailand" = "red", "Other" = "gray")) +
  annotate(geom = "text", x = 10000, y = 70, label = "Thailand", color = "red", size = 5, fontface = "bold") +
  labs(title = "GDP per Capita vs Life Expectancy (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme(legend.position = "none")

Task:

  • Filter year 2007 from Gapminder.

  • Set a dark background with light-colored elements.

  • Change:

    • Panel background: Dark gray.

    • Grid lines: Light blue.

    • Text: White.

Solution:

gapminder_2007 |> 
ggplot() +
aes(x = gdpPercap, y = lifeExp, color = continent) +
  geom_point(size = 3) +
  labs(title = "GDP per Capita vs Life Expectancy (2007)",
       x = "GDP per Capita",
       y = "Life Expectancy") +
  theme(
    panel.background = element_rect(fill = "black"),
    plot.background = element_rect(fill = "gray20"),
    axis.title = element_text(color = "white"),
    axis.text = element_text(color = "white"),
    panel.grid.major = element_line(color = "lightblue"),
    panel.grid.minor = element_line(color = "lightblue", linetype = "dashed"),
    legend.background = element_rect(fill = "gray30"),
    legend.text = element_text(color = "white"),
    plot.title = element_text(size = 18, face = "bold", color = "white")
  )