Using ggplot2
ggplot2 uses the idea that you can build every graph from the same components:
a data set
a coordinate system
geoms - visual marks that represent data
Complete the template below to build a graph
ggplot(data = mpg, aes(x = class, y = hwy))
This will begin a plot that you can finish by adding layers to.
You can add one geom per layer
Change the code below to have the points on top of the boxplots.
In ggplot2, we use a geom function to represent data points, and use the geom’s aesthetic properties to represent variables.
Once our data is formatted and we know what type of variables we are working with, we can select the correct geom for our visualization.
it determines the physical representation of the data
Together, the data, mappings, statistical transformation, and geometric object form a layer
A plot may have multiple layers
A stat builds a new variable to plot (e.g., count and proportion)
A way to extract subsets of data and place them side-by-side in graphics
facet_grid(. ~ b)
: facet into columns based on bfacet_grid(a ~ .)
: facet into rows based on afacet_grid(a ~ b)
: facet into both rows and columnsfacet_wrap( ~ fl)
: wrap facets into a rectangular layoutYou can set scales to let axis limits vary across facets:
facet_grid(y ~ x, scales = "free")
: x and y axis limits adjust to individual facets
You can also set a labeller to adjust facet labels: - facet_grid(. ~ fl, labeller = label_both)
- facet_grid(. ~ fl, labeller = label_bquote(alpha ^ .(x)))
- facet_grid(. ~ fl, labeller = label_parsed)
Position adjustments determine how to arrange geoms that would otherwise occupy the same space - Dodge: Arrange elements side by side - Fill: Stack elements on top of one another, normalize height - Stack: Stack elements on top of one another
coord_cartesian()
: The default cartesian coordinate systemcoord_fixed()
: Cartesian with fixed aspect ratio between x & y unitscoord_flip()
: Flipped Cartesian coordinatescoord_polar()
: Polar coordinatescoord_trans()
: Transformed cartesian coordinates.coord_map()
: Map projections from the mapproj package (mercator (default), azequalarea, lagrange, etc.)Plotting geospatial data is a common visualization task, and one that requires specialized tools.
Typically the problem can be decomposed into two problems:
using one data source to draw a map
adding metadata from another information source to the map
Map Source Data
long | lat | group | order | region | subregion |
---|---|---|---|---|---|
-98.72636 | 40.69719 | 1 | 1 | nebraska | adams |
-98.29664 | 40.69719 | 1 | 2 | nebraska | adams |
Population Data
subregion | population |
---|---|
adams | 31205 |
antelope | 6295 |
Check out slides for some frequently used extensions by Ashirwad Barnwal HERE.