geomerrorbar and geomerrorbarh¤
Error bars display uncertainty in data values by showing a range around each point. geom_errorbar() creates vertical error bars, while geom_errorbarh() creates horizontal error bars. These are commonly used to show confidence intervals, standard errors, or other measures of variability around summary 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.Rangebars)
Vertical vs Horizontal¤
Use geom_errorbar() when your error bars should be vertical (most common case, e.g., when x is categorical and y is numeric). Use geom_errorbarh() when your error bars should be horizontal (e.g., when y is categorical and x is numeric).
Required Aesthetics (geom_errorbar)¤
x- the x position of the error barymin- the lower end of the error barymax- the upper end of the error bar
Required Aesthetics (geom_errorbarh)¤
y- the y position of the error barxmin- the left end of the error barxmax- the right end of the error bar
Optional Aesthetics (see aes)¤
color/colour
Optional Arguments¤
color/colour: color of the error barslinewidth: width of the error bar lineswhiskerwidth/width: width of the whisker caps (in pixels)alpha
using TidierPlots
using TidierData
using DataFrames
using Statistics
penguins = penguins_data();
First, create a summary dataset with means and standard deviations:
df = @chain penguins begin
@group_by(species)
@summarize(
mean_bill = mean(bill_length_mm),
sd_bill = std(bill_length_mm),
n = n()
)
@mutate(
se_bill = sd_bill / sqrt(n)
)
@mutate(
lower = mean_bill - 1.96 * se_bill,
upper = mean_bill + 1.96 * se_bill
)
end
| Row | species | mean_bill | sd_bill | n | se_bill | lower | upper |
|---|---|---|---|---|---|---|---|
| String | Float64 | Float64 | Int64 | Float64 | Float64 | Float64 | |
| 1 | Adelie | 38.824 | 2.6626 | 146 | 0.220358 | 38.3921 | 39.2559 |
| 2 | Gentoo | 47.5681 | 3.10612 | 119 | 0.284737 | 47.01 | 48.1262 |
| 3 | Chinstrap | 48.8338 | 3.33926 | 68 | 0.404944 | 48.0401 | 49.6275 |
Basic error bars showing 95% confidence intervals:
ggplot(df, @aes(x = species, ymin = lower, ymax = upper)) +
geom_errorbar(linewidth = 2)
Add whisker caps to make the endpoints more visible:
ggplot(df, @aes(x = species, ymin = lower, ymax = upper)) +
geom_errorbar(linewidth = 2, whiskerwidth = 20)
Combine error bars with points to show both the central value and uncertainty:
ggplot(df, @aes(x = species, y = mean_bill, ymin = lower, ymax = upper)) +
geom_errorbar(linewidth = 1, whiskerwidth = 15) +
geom_point(size = 15)
Combine with bar charts:
ggplot(df, @aes(x = species, y = mean_bill, ymin = lower, ymax = upper)) +
geom_col(alpha = 0.5) +
geom_errorbar(linewidth = 2, whiskerwidth = 20)
Create horizontal error bars using geom_errorbarh:
df_h = @chain penguins begin
@group_by(species)
@summarize(
mean_bill = mean(bill_length_mm),
sd_bill = std(bill_length_mm),
n = n()
)
@mutate(
se_bill = sd_bill / sqrt(n)
)
@mutate(
lower = mean_bill - 1.96 * se_bill,
upper = mean_bill + 1.96 * se_bill
)
end
ggplot(df_h, @aes(y = species, xmin = lower, xmax = upper)) +
geom_errorbarh(linewidth = 2)
Example with custom error bar data:
df_custom = DataFrame(
trt = [1, 1, 2, 2],
resp = [1, 5, 3, 4],
group = ["A", "B", "A", "B"],
lower = [0.8, 4.6, 2.4, 3.6],
upper = [1.1, 5.3, 3.3, 4.2]
)
ggplot(df_custom, @aes(x = trt, y = resp, ymin = lower, ymax = upper, color = group)) +
geom_errorbar(whiskerwidth = 20, linewidth = 2) +
geom_point(size = 15)
This page was generated using Literate.jl.