Aan de slagGa gratis aan de slag

Data columns types affect plot types

The plot from the previous exercise wasn't really satisfying. Although cyl (the number of cylinders) is categorical, you probably noticed that it is classified as numeric in mtcars. This is really misleading because the representation in the plot doesn't match the actual data type. You'll have to explicitly tell ggplot2 that cyl is a categorical variable.

Deze oefening maakt deel uit van de cursus

Introduction to Data Visualization with ggplot2

Cursus bekijken

Oefeninstructies

  • Change the ggplot() command by wrapping factor() around cyl.
  • Submit the code and see if the resulting plot is better this time.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Load the ggplot2 package
library(ggplot2)

# Change the command below so that cyl is treated as factor
ggplot(mtcars, aes(cyl, mpg)) +
  geom_point()
Code bewerken en uitvoeren