I would like change the patterns in my stacked barchart using scale_pattern_manual
to have the last stack in my stacked barchart have vertical lines instead of horizontal 45 degrees. So I want them to be 45 degree horizontal lines, crosshatch, 180 degree vertical lines. Is there an easy way to do this? I tried to add pattern_angle = c(45, 45, 180)
inside geom_col_pattern()
but am getting an error as it will only let me add one angle (i.e., pattern_angle = 180) which will change the line angle for all three variables.
set.seed(687532) # Create example data frame
data <- data.frame(facet = rep(LETTERS[1:5], each = 6),
group = c("x", "y"),
stack = letters[1:3],
value = round(abs(rnorm(30)), 2))
data
ggplot(data, # Draw barplot with grouping & stacking
aes(x = group,
y = value,
fill = stack)) +
geom_bar(stat = "identity",
position = "stack", width = 0.6) +
theme_classic() + theme(panel.border = element_rect(colour = NA, fill=NA, size=0),
text = element_text(size = 8, family = "sans"),
) + scale_y_continuous(expand = c(0,0)) + labs(x = "", y = expression(paste("Relative Abundance(%)"))) +
facet_wrap(~facet, strip.position = "bottom", scales = "free_x") +
theme(panel.background = element_blank(),
panel.spacing = unit(0, "line"),
strip.background = element_blank(),
strip.placement = "outside") + theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5)) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
scale_x_discrete(NULL, expand = c(0.3, 0.3)) + scale_fill_manual(values = c('black', 'lightgrey', 'darkgrey')) +
scale_pattern_manual(
"stack",
values = c('stripe', 'crosshatch', 'stripe')
) +
geom_col_pattern(
aes(pattern = stack),
pattern_size = .05,
pattern_density = .1,
pattern_spacing = .02,
pattern_fill = "black",
color = "black",
)
You can achieve your desired result by mapping on the pattern_angle
aesthetic and setting your desired angles via scale_pattern_angle_manual
:
Note: For the sake of visibility I have used pattern_fill = "red"
and pattern_spacing = .05
. And as a general note: geom_col_pattern
is a substitute for geom_col
or geom_bar
, i.e. there is no need to add duplicated layers.
library(ggpattern)
library(ggplot2)
ggplot(
data,
aes(
x = group,
y = value,
fill = stack
)
) +
geom_col_pattern(
aes(pattern = stack, pattern_angle = stack),
pattern_size = .05,
pattern_density = .1,
pattern_spacing = .05,
pattern_fill = "red",
color = "black",
) +
scale_y_continuous(expand = c(0, 0)) +
scale_x_discrete(NULL, expand = c(0.3, 0.3)) +
scale_fill_manual(values = c("black", "lightgrey", "darkgrey")) +
scale_pattern_manual(
"stack",
values = c("stripe", "crosshatch", "stripe")
) +
scale_pattern_angle_manual(
"stack",
values = c(45, 30, 90)
) +
facet_wrap(~facet, strip.position = "bottom", scales = "free_x") +
theme_classic() +
theme(
panel.border = element_rect(colour = NA, fill = NA, size = 0),
text = element_text(size = 8, family = "sans"),
panel.background = element_blank(),
panel.spacing = unit(0, "line"),
strip.background = element_blank(),
strip.placement = "outside",
plot.title.position = "plot",
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)
) +
labs(x = "", y = expression(paste("Relative Abundance(%)")))