R Bootcamp - Day 4
ggplot2
ggplot2
Today’s datasets
In this class, we will use a data set from ggplot2: diamonds
contains thousands of gem prices and qualities.
There are many interesting data sets you can install as R packages for learning to manipulate and plot data:
Getting familiar with the data - Exercise 1
The syntax of ggplot()
Making a plot step-by-step (Exercise 2)
Initialize a plot with data.
Next, specify the coordinate system.
Add a geom (geom_point
).
Map aesthetics to other variables.
Reduce overplotting by adjusting the transparency of points.
Looking under the hood of ggplot (Exercise 3)
ggplot is powerfully simple for making complex plots
Why can’t I just do this?
Geom functions
- Use a geom function to represent data points, use the geom aesthetic properties to represent variables.
- Each function returns a plot layer.
- There are many geoms in ggplot that are specific to plots with 1, 2, or 3 variables
Make a bar plot.
Update the bar plot aesthetics.
Change to a density plot.
Color the density plot.
Plot subsets by mapping fill
to cut
Use ggridges
to plot staggered subsets.
https://wilkelab.org/ggridges/
Geom functions for two variables
Make a column plot.
Same data with a box plot.
Box plot, with fill color by cut.
Violin plot with fill color by cut.
continuous x, continuous y - Exercise 6
Subset diamonds to see points more clearly.
Make a scatter plot.
Now add a smoothing line.
Here we can combine geoms to see points & the fit