2.1 Exercises
- Why does this code not work?
Because the name on the second line doesn’t include and
i
. Remember, typos matter.
- Tweak each of the following R commands so that they run correctly:
libary(todyverse)
ggplot(dTA = mpg) +
geom_point(maping = aes(x = displ y = hwy)) +
geom_smooth(method = "lm)
Here is the correct code with no typos:
library(tidyverse)
ggplot(data = mpg)+
geom_point(mapping = aes(x = display, y = hwy)) +
geom_smooth(method = "lm")
- Press Option + Shift + K / Alt + Shift + K. What happens? How can you get to the same place using the menus?
This takes you to the list of shortcuts. Another way of getting here is: Tools > Keyboard shortcuts help.
- Let’s revisit an exercise from the Section 2.6. Run the following lines of code. Which of the two plots is saved as mpg-plot.png? Why?
my_bar_plot <- ggplot(mpg, aes(x = class)) +
geom_bar()
my_scatter_plot <- ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point()
ggsave(filename = "mpg-plot.png", plot = my_bar_plot)
The arguments for ggsave are (
?ggsave
in the console): filename, plot, device, path, …). Plot refers to the plot to be saved, so in this case the first plot namedmy_bar_plot
will be saved as mpg-plot.png.