2.1 Exercises

  1. Why does this code not work?
my_variable <- 10
#> Error in eval(expr, envir, enclos): object 'my_varıable' not found

Because the name on the second line doesn’t include and i. Remember, typos matter.

  1. Tweak each of the following R commands so that they run correctly:

ggplot(dTA = mpg) + 
  geom_point(maping = aes(x = displ y = hwy)) +
  geom_smooth(method = "lm)

Here is the correct code with no typos:


ggplot(data = mpg)+
  geom_point(mapping = aes(x = display, y = hwy)) +
  geom_smooth(method = "lm")
  1. 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.

  1. 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)) +
my_scatter_plot <- ggplot(mpg, aes(x = cty, y = hwy)) +
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 named my_bar_plot will be saved as mpg-plot.png.