df <- data.frame(x1 = c("a", "a", "b", "b", "b"),
x2 = c(1, 2, 1, 3, -1),
grp = c("some long name", "other name", "some name",
"another name", "some long name"))
# Add labels to a horizontal column plot (stacked by default)
ggplot(data = df, aes(x1, x2, group = grp)) +
geom_col(aes(fill = grp), width=0.5) +
geom_vline(xintercept = 0) +
geom_text(
aes(label = grp),
position = position_stacknudge(vjust = 0.5, y = 0.3)) +
theme(legend.position = "none")
# Add labels to a vertical column plot (stacked by default)
ggplot(data = df, aes(x2, x1, group = grp)) +
geom_col(aes(fill = grp), width=0.5) +
geom_vline(xintercept = 0) +
geom_text(
aes(label = grp),
position = position_stacknudge(vjust = 0.5, x = -0.3),
angle = 90) +
theme(legend.position = "none")
# Add labels to a vertical column plot (stacked by default)
ggplot(data = subset(df, x1 >= 0), aes(x1, x2, group = grp)) +
geom_col(aes(fill = grp), width=0.5, position = position_fill()) +
geom_vline(xintercept = 0) +
geom_text(
aes(label = grp),
position = position_fillnudge(vjust = 0.5, x = -0.3),
angle = 90) +
theme(legend.position = "none")
# Add label at a fixed distance from the top of each column slice
ggplot(data = df, aes(x1, x2, group = grp)) +
geom_col(aes(fill = grp), width=0.5) +
geom_vline(xintercept = 0) +
geom_text(
aes(label = grp),
position = position_stacknudge(vjust = 1, y = -0.2)) +
theme(legend.position = "none")
# Use geom_text_s(), geom_text_repel() or geom_label_repel() to link
# label to labelled segment or object with an arrow
ggplot(data = df, aes(x2, x1, group = grp)) +
geom_col(aes(fill = grp), width=0.5) +
geom_vline(xintercept = 0) +
geom_text_s(
aes(label = grp),
position = position_stacknudge(vjust = 0.5, y = 0.35),
vjust = "bottom") +
theme(legend.position = "none")
ggplot(birch_dw.df,
aes(y = dry.weight * 1e-3, x = Density, fill = Part)) +
stat_summary(geom = "col", fun = mean,
position = "stack", alpha = 0.7, width = 0.67) +
stat_summary(geom = "linerange", fun.data = mean_cl_normal,
position = position_stack_minmax()) +
labs(y = "Seedling dry mass (g)") +
scale_fill_grey(start = 0.7, end = 0.3) +
facet_wrap(facets = vars(Container))
Run the code above in your browser using DataLab