Unit 3Data Visualization With Ggplot2
Unit 3Data Visualization With Ggplot2
• You might use stat_summary(), which summarizes the y values for each
unique x value, to draw attention.
ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median)
Position Adjustments
You can color a bar chart using either the color aesthetic, or
by fill = x axes variable.
clarity: the bars are automatically stacked.
The stacking is performed automatically by the position adjustment specified
by the position argument. So ggplot is providing 3 such position options.
They are: "identity", "dodge" or "fill".
position = "identity“:
It will place each object exactly where it falls in the context of the graph.
This is not very useful for bars, because it overlaps them.
To see that overlapping we either need to make the bars slightly transparent
by setting alpha to a small value, or completely transparent by setting fill =
NA
position = "fill":
It works like stacking, but makes each set of stacked bars the same height.
This makes it easier to compare proportions across groups:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
position = "dodge":
It places overlapping objects directly beside one another. This makes it easier
to compare individual values:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity),
position = "dodge")
Coordinate Systems
Coordinate systems are probably the most complicated part of ggplot2.
The default coordinate system is the Cartesian coordinate system where the x
and y position act independently to find the location of each point.
There are a number of other coordinate systems that are occasionally helpful:
coord_flip(): It switches the x- and y-axes, if you want horizontal
boxplots. It’s also useful for long labels.
• coord_quickmap() sets the aspect ratio correctly for maps. This is very
important if you’re plotting spatial data with ggplot2. Example: maps