Classwork 4

The Layered Grammar of Graphics

Author

Byeong-Hak Choe

Published

February 3, 2025

Modified

February 9, 2025

Question 1. Geometric Objects

Q1a.

Run this code in your head and predict what the output will look like. Then, run the code in R and check your predictions.

ggplot(data = mpg, 
       mapping = aes(x = displ, y = hwy, 
                     color = drv)) +
  geom_point(alpha = .4) +
  geom_smooth(se = FALSE)


Q1b.

What does show.legend = FALSE do? What happens if you remove it?

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, 
                           color = drv),
             alpha = .5,
             show.legend = FALSE)

Answer:


Q1c.

Recreate the R code necessary to generate the following graphs.

(a)


(b)


(c)


(d)


(e)


(f)


Q1d.

  • Use the data.frame, organdata_simple to visualize the yearly trend of the variable donors for each country.
organdata_simple <- read_csv('https://bcdanl.github.io/data/organdata_simple.csv')




Question 2. Statistical Transformation and Position Adjustment

Q2a.

What does geom_col() do? How is it different to geom_bar()?

Answer:



Q2b.

  • Install the R package, nycflights13, which provides the flights data.frame.
flights <- nycflights13::flights
airlines <- nycflights13::airlines

flights <- flights |> 
  left_join(airlines)
  • Visualize the distribution of carrier.

  • Visualize how the distribution of carrier varies by origin.

Answer:



Question 3. Work a little more with gapminder data

library(gapminder)
p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp))
p + 
  geom_point() +
  geom_smooth()

Q3a.

  • What happens when you put the geom_smooth() function before geom_point() instead of after it?

  • What does this tell you about how the plot is drawn? Think about how this might be useful when drawing plots.



Q3b.

  • Change the mappings in the aes() function so that you plot Life Expectancy against population (pop) rather than per capita GDP.
    • What does that look like?
    • What does it tell you about the unit of observation in the dataset?



Q3c.

  • What happens if you map color to year instead of continent?
    • Is the result what you expected?
    • Think about what class of object year is.
    • Remember you can get a quick look at the top of the data, which includes some shorthand information on the class of each variable, by typing gapminder.
    • Instead of mapping color = year, what happens if you try color = factor(year)?



Discussion

Welcome to our Classwork 4 Discussion Board! 👋

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

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