geom_col¤
geom_col() creates bar charts where the heights of the bars represent values in the data. Unlike geom_bar(), which counts the number of cases, geom_col() displays the actual data values. This makes it useful for showing pre-summarized data such as means, totals, or other statistics.
Arguments¤
plot::GGPlot(optional): a plot object to add this geom to. This is typically used to facilitate creating your ggplot as part of a @chain.data(DataFrame): Data to use for this geom. If not provided, the geom will inherit the data from ggplot.aes(...): the names of the columns in the DataFrame that will be used in the mappinginherit_aes: should the geom inherit aes from the ggplot?...: options that are not mapped to a column (passed to Makie.BarPlot)
Stacking and Dodging¤
By default, multiple bars at the same position are stacked on top of each other. You can change this behavior with the position argument. Use position="dodge" to place bars side-by-side. When using position="dodge", bars will be grouped by the fill, color, group, or dodge aesthetic (checked in that order).
When to Use geomcol vs geombar¤
- Use
geom_bar()when you want to count the number of observations in each category (the data is unsummarized) - Use
geom_col()when you have pre-computed values you want to display (the data is already summarized)
Required Aesthetics¤
x- the categorical variabley- the numeric value to display as bar height
Optional Aesthetics (see aes)¤
fill(maps to bar interior color)color/colour(maps to bar outline color)dodge(grouping variable for side-by-side bars)group(grouping variable)
Optional Arguments¤
position: "stack" (default) or "dodge"color/colourstroke/strokewidthstrokecolor/strokecolourdirection::y(default) or:xdodge_gap: gap between dodged barsgap: gap between bar groupsalpha
using TidierPlots
using TidierData
using DataFrames
penguins = penguins_data();
First, we create a summarized dataset with mean bill length by species and sex:
df = @chain penguins begin
@group_by(species, sex)
@summarize(mean_bill_length_mm = mean(bill_length_mm))
@ungroup()
end
| Row | species | sex | mean_bill_length_mm |
|---|---|---|---|
| String | String | Float64 | |
| 1 | Adelie | male | 40.3904 |
| 2 | Adelie | female | 37.2575 |
| 3 | Gentoo | female | 45.5638 |
| 4 | Gentoo | male | 49.4738 |
| 5 | Chinstrap | female | 46.5735 |
| 6 | Chinstrap | male | 51.0941 |
Basic bar chart showing pre-computed values:
ggplot(df, @aes(x = species, y = mean_bill_length_mm)) +
geom_col()
Use the fill aesthetic to color bars by a grouping variable. Bars are stacked by default:
ggplot(df, @aes(x = species, y = mean_bill_length_mm, fill = sex)) +
geom_col()
Use position="dodge" with the group aesthetic to place bars side-by-side:
ggplot(df, @aes(x = species, y = mean_bill_length_mm, group = sex)) +
geom_col(position = "dodge")
You can also use the dodge aesthetic directly:
ggplot(df, @aes(x = species, y = mean_bill_length_mm, dodge = sex)) +
geom_col()
Combine fill with position dodge for colored side-by-side bars:
ggplot(df, @aes(x = species, y = mean_bill_length_mm, fill = sex, dodge = sex)) +
geom_col()
Customize appearance with stroke and transparency:
ggplot(df, @aes(x = species, y = mean_bill_length_mm, fill = sex)) +
geom_col(strokecolor = :black, strokewidth = 1, alpha = 0.8, position = "dodge")
Create horizontal bars by using direction = :x:
df_single = @chain penguins begin
@group_by(species)
@summarize(mean_bill_length_mm = mean(bill_length_mm))
end
ggplot(df_single, @aes(x = species, y = mean_bill_length_mm)) +
geom_col(direction = :x)
This page was generated using Literate.jl.