Aesthetics
Aesthetics for bars
geom_bar()
and geom_col()
can use several aesthetics:
alpha
color
fill
linetype
size
One of these, color
, creates the most surprising results. Predict what the code below will return and then run it.
fill
The color
aesthetic controls the outline of each bar in your bar plot, which may not be what you want. To color the interior of each bar, use the fill
aesthetic:
Use the code chunk below to experiment with fill, along with other geom_bar()
aesthetics, like alpha
, linetype
, and size
.
Width
You can control the width of each bar in your bar chart with the width
parameter. In the chunk below, set width = 1
, then width = 0.5
. Can you spot the difference?
Notice that width is a parameter, not an aesthetic mapping. Hence, you should set width outside of the aes()
function.
Exercise 5: Aesthetics
Create a colored bar chart of the class
variable from the mpg
data set, which comes with ggplot2. Map the interior color of each bar to class
.
ggplot(data = mpg) +
geom_bar(mapping = aes(x = class, fill = class))