Classwork 6

Visualization Practice

Author

Byeong-Hak Choe

Published

February 24, 2025

Modified

April 6, 2025

Question 1

The following data is for Question 1:

gapminder <- gapminder::gapminder


Q1a

  • Replicate the following ggplot.
    • Use the color #0072B2 for dots.
Click to Check the Answer!
# Set the data and filter to include only observations from 2007 and exclude Oceania
ggplot(data = filter(gapminder, year == 2007,
                     continent != 'Oceania'),

       # Set the aesthetics (x-axis and y-axis) to life expectancy and reorder countries by life expectancy
       aes(x = lifeExp,
           y = fct_reorder(country, lifeExp))) +

  # Add a layer of points to the plot, setting the color to blue and size to 1.75
  geom_point(color = "#0072B2", size = 1.75) +

  # Add a layer of text labels to the plot, setting the label to life expectancy, hjust to -.25, and size to 2
  geom_text(aes(label = lifeExp), hjust = -.25,
            size = 2) +

  # Facet the plot by continent, with y-scales free
  facet_wrap(.~continent, scales = "free_y") +

  # Set the x-axis label to NULL (no name) and limit the x-axis to 35-90
  scale_x_continuous(
    name = NULL,
    lim = c(35, 90)
  ) +

  # Set the y-axis label to NULL (no name)
  scale_y_discrete(name = NULL) +

  # Add a title to the plot
  labs(title = 'Life expectancy in 2007') +

  # Set the theme to minimal
  theme_minimal() +

  # Customize theme elements: set the y-axis text size, the plot title size and position, and the facet strip text size and font face
  theme(
    axis.text.y = element_text(size = rel(.75)),
    plot.title = element_text(size = rel(1.5),
                              hjust = 0.5,
                              face = 'bold'),
    strip.text = element_text(size = rel(1.25),
                              face = 'bold')
  )



Q1b

  • Make a simple comment on the visualization result.

    • Any comment that is not made up is okay.




Question 2

The following data set is for Question 2:

electricity <- read_csv(
  'https://bcdanl.github.io/data/electricity-usa-chn.csv')


Q2a

  • Replicate the following ggplot.
Click to Check the Answer!
ggplot(data = electricity,
       aes(x = year, y = value)) + 
  geom_line(aes(color = energy),
            linewidth = 1.25) +
  geom_hline(yintercept = 0) +  
  geom_vline(xintercept = min(electricity$year)) +
  facet_wrap(.~ iso3c, 
             labeller = as_labeller(c('CHN' = "China",
                                      "USA" = "United States"))) +
  scale_y_comma() +
  scale_color_viridis_d(option = "C") +
  guides(color = guide_legend(label.position = "bottom",
                             keywidth = 3,
                             nrow = 1)) +
  labs(x = "Year", y = "Electricity\nGeneration\n(TWh)",
       color = "Energy") +
  hrbrthemes::theme_ipsum() +
  theme(axis.title.y = element_text(size = rel(1.25),
                                    angle = 0),
        axis.title.x = element_text(size = rel(1.25),
                                    angle = 0),
        strip.background = element_rect(fill = 'grey90',
                                        colour = NA),
        legend.position = 'top')



Q2b

  • Replicate the following ggplot.
Click to Check the Answer!
electricity2 <- electricity |> 
  group_by(iso3c, year) |> 
  mutate(tot = sum(value),
         prop = value / tot)


ggplot(data = electricity2,
       aes(x = year, y = prop)) + 
  geom_line(aes(color = energy),
            linewidth = 1.25) +
  geom_hline(yintercept = 0) +  
  geom_vline(xintercept = min(electricity$year)) +
  facet_wrap(.~ iso3c, 
             labeller = as_labeller(c('CHN' = "China",
                                      "USA" = "United States"))) +
  scale_y_percent() +
  scale_color_viridis_d(option = "D") +
  guides(color = guide_legend(label.position = "bottom",
                              keywidth = 3,
                              nrow = 1)) +
  labs(x = "Year", y = "Percentage of\nElectricity\nGeneration\n(TWh)",
       color = "Energy") +
  hrbrthemes::theme_ipsum() +
  theme(axis.title.y = element_text(size = rel(1.25),
                                    angle = 0),
        axis.title.x = element_text(size = rel(1.25),
                                    angle = 0),
        strip.background = element_rect(fill = 'grey90',
                                        colour = NA),
        legend.position = 'top')




Question 3

The following data is for Question 3:

n_tweets_long <- read_csv(
  'https://bcdanl.github.io/data/n_tweets_long.csv')


Q3a

  • Replicate the following ggplot.

    • The following describes the type values:
      • n_ot_us: Number of US tweets
      • n_ot_wrld: Number of worldwide tweets
      • n_rt_lk_us: Number of US retweets & likes
      • n_rt_lk_wrld: Number of worldwide retweets & likes
    • Use the colors, maroon and #428bca properly.
Click to Check the Answer!
n_tweets_long1 <- n_tweets_long |> 
  filter(type %in% c("n_ot_us", "n_ot_wrld") ) |> 
  mutate(type = ifelse(type == "n_ot_us", "US", "Worldwide"))

n_tweets_long2 <- n_tweets_long |> 
  filter(type %in% c("n_rt_lk_us", "n_rt_lk_wrld") ) |> 
  mutate(type = ifelse(type == "n_rt_lk_us", "US", "Worldwide"))

p2 <- ggplot(mapping = aes(x = year, y = n)) +
  geom_col(n_tweets_long1,
           mapping = aes(fill = type),
           position = 'dodge', alpha = .67) + 
  geom_line(n_tweets_long2,  
            mapping = aes(color = type),
            size = 1.5) +  
  geom_point(data = n_tweets_long2,  
             size = 2,  
             color = 'black')  +  
  scale_x_continuous(breaks = seq(2012, 2017, 1)) +  
  scale_y_continuous(label = scales::comma) +  
  scale_color_manual(values = c('maroon', '#428bca')) +  
  scale_fill_manual(values = c('maroon', '#428bca')) +   
  guides(fill = guide_legend(reverse = TRUE, 
                             label.position = "bottom",
                             keywidth = 2,
                             nrow = 2,
                             order = 1),
         color = guide_legend(reverse = TRUE,  
                             label.position = "bottom",
                             keywidth = 2,
                             nrow = 2,
                             order = 2)) +
  labs(x = "Year", 
       y = "Number of Tweets, Retweets & Likes\n (in thousand)",  
       fill = "Tweets",  
       color = "Retweets and likes",  
       caption = 'Source: Choe, "Social Media Campaigns, Lobbying, and Climate Change Legislation:\n Evidence from #climatechange/#globalwarming and Energy Lobbies" (2023)') +  
  theme_ipsum() + 
  theme(
  axis.title.y = element_text(
    size = rel(1.5),
    margin = margin(t = 0, r = 20, b = 0, l = 0) 
  ),
  axis.title.x = element_text(
    size = rel(1.5),
    margin = margin(t = 10, r = 0, b = 0, l = 0) 
  ),
  axis.text.x = element_text(
    size = rel(1.25) 
  ),
  axis.text.y = element_text(
    size = rel(1.25) 
  ),
  legend.position = 'top', 
  legend.title = element_text(
    size = rel(1),
    face = 'bold',
    hjust = .5 
  ),
  legend.text = element_text(
    size = rel(1) 
  ),
  legend.spacing.x = unit(1.25, "cm"), 
  plot.caption = element_text(
    size = rel(1.25),
    hjust = .5 
  )
)


p2



Q3b.

  • Make a simple comment on the visualization result.




Discussion

Welcome to our Classwork 6 Discussion Board! 👋

This space is designed for you to engage with your classmates about the material covered in Classwork 6.

Whether you are looking to delve deeper into the content, share insights, or have questions about the content, this is the perfect place for you.

If you have any specific questions for Byeong-Hak (@bcdanl) regarding the Classwork 6 materials or need clarification on any points, don’t hesitate to ask here.

All comments will be stored here.

Let’s collaborate and learn from each other!

Back to top