I'm a beginner with R going over the ggplot2 tutorial and something has caught my eye as being bizarre when using the mtcars dataset.
For example, consider the following:
>library(ggplot2)
>g<-ggplot(mpg, aes(class)) + geom_bar()
>g
I can't figure out why this works. This clearly makes a plot with the counts of each car class (2seater, compact, midsize, minivan, pickup, subcompact, suv).
My question is: How does R/ggplot know what classes these cars are in? There is no variable in the mtcars data.frame that describes this:
>mtcars$class
NULL
Is this something just built into the ggplot package?
mpg$class
– HubertLmpg
data frame in yourggplot
code, not themtcars
data frame. Thempg
data frame is built into the ggplot2 package (rundata(package="ggplot2")
), while themtcars
data frame is included in base R. – eipi10