ggplot(data = mpg,
mapping = aes(x = displ, y = hwy,
color = drv)) +
geom_point(alpha = .4) +
geom_smooth(se = FALSE)
Classwork 4
The Layered Grammar of Graphics
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.
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 variabledonors
for each country.
<- read_csv('https://bcdanl.github.io/data/organdata_simple.csv') organdata_simple
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 theflights
data.frame.
<- nycflights13::flights
flights <- nycflights13::airlines
airlines
<- flights |>
flights left_join(airlines)
Visualize the distribution of
carrier
.Visualize how the distribution of
carrier
varies byorigin
.
Answer:
Question 3. Work a little more with gapminder data
library(gapminder)
<- ggplot(data = gapminder,
p mapping = aes(x = gdpPercap,
y = lifeExp))
+
p geom_point() +
geom_smooth()
Q3a.
What happens when you put the
geom_smooth()
function beforegeom_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
toyear
instead ofcontinent
?- 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 trycolor = 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!