Please sign up for a function here: https://docs.google.com/spreadsheets/d/1-RWAQTlLwttjFuZVAtSs8OiHIwu6AZLUdWugIHHTWVo/edit?usp=sharing
For this assignment, please submit both the .Rmd
and the
.html
files. I will add it to the website. Remove your name
from the Rmd if you do not wish it shared. If you select a function
which was presented last
year, please develop your own examples and content.
expand_limits()
In this document, I will introduce the expand_limits() function and show what it’s for.
#load tidyverse up
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
#example dataset
library(palmerpenguins)
data(penguins)
Discuss what the function does. Learn from the examples, but show how to use it using another dataset such as
penguins
. If you can provide two examples, even better!
# Used to define the limits of a single value, for both panels and plots, able to wrap around any geom function
peng_mass <- ggplot(penguins,
aes (x = body_mass_g,
y = flipper_length_mm,
fill = species)) +
geom_col()
peng_mass + expand_limits(y = c(1, 5000))
## Warning: Removed 2 rows containing missing values (`position_stack()`).
# Sometimes you can use expand_limits() to change the range of both x and y values!
peng_mass + expand_limits(x = 0, y = 0)
## Warning: Removed 2 rows containing missing values (`position_stack()`).
Discuss whether you think this function is useful for you and your work. Is it the best thing since sliced bread, or is it not really relevant to your work?
I would say that it is helpful to adjust the values of the x and y axis manually if you do not want ggplot to automatically set the origin to the lowest value. expand_limits() can also allow you to change the color according to the range you highlight.
Reference: https://ggplot2.tidyverse.org/reference/expand_limits.html