library(ggplot2)
data("mpg")
qplot(displ, hwy, data = mpg, color = drv )
qplot(displ, hwy, data = mpg, color = drv, geom = c("point", "smooth") )
## `geom_smooth()` using method = 'loess'
qplot(y=hwy, data = mpg, color = drv)
The all-purpose qplot can also create box and whisker plots. Call | qplot now with 4 arguments. First specify the variable by which | you’ll split the data, in this case drv, then specify the variable | which you want to examine, in this case hwy. The third argument is | data (set equal to mpg), and the fourth, the geom, set equal to the | string “boxplot”
qplot(drv, hwy, data = mpg, geom = "boxplot")
qplot(drv, hwy, data = mpg, geom = "boxplot", color = manufacturer)
histogram
qplot(hwy, data = mpg, fill=drv)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
We’ll do two plots, a scatterplot and then a histogram, each with 3 facets. For the scatterplot, call qplot with 4 arguments. The first two are displ and hwy and the third is the argument data set equal to mpg. The fourth is the argument facets which will be set equal to the expression . ~ drv which is ggplot2’s shorthand for number of rows (to the left of the ~) and number of columns (to the right of the ~). Here the . indicates a single row and drv implies 3, since there are 3 distinct drive factors.
qplot(displ, hwy, data = mpg, facets = . ~ drv)
This shows us more detailed information than the histogram. We see the relationship between displacement and highway mileage for each of the 3 drive factors.
Now we’ll do a histogram, again calling qplot with 4 arguments. This | time, since we need only one variable for a histogram, the first is | hwy and the second is the argument data set equal to mpg. The third | is the argument facets which we’ll set equal to the expression drv ~ | . . This will give us a different arrangement of the facets. The | fourth argument is binwidth. Set this equal to 2.
qplot(hwy, data = mpg, facets=drv~., binwidth = 2)