Filling bars with color
ggplot(___) +
geom_col(
mapping = aes(x = ___, y = ___,
fill = ___)
)
View Interactive VersionLike other geoms geom_col()
allows users to map additional dataset variables to the color attribute of the bar. The fill
aesthetic can be used to fill the entire bars with color. A usual confusion is the color
aesthetic which specifies the line color of each bar’s border instead of the fill color.
Based on the gapminder_top5
dataset we plot the population (in millions) of the biggest countries and use the continent
variable to color each bar:
ggplot(gapminder_top5) + geom_col(aes(x = country, y = pop, fill = continent))

Since the continent
variable is a categorical variable the bars have a clear color scheme for each continent. Let’s see what happens if we use a numeric
variable like life expectancy lifeExp
instead:
ggplot(gapminder_top5) + geom_col(aes(x = country, y = pop, fill = lifeExp))

The bar colors have now changed according the continuous legend on the right. We see that also numeric
variables can be used to fill
bars.