Based on the ggplot2 package, I want to add different textures to different bars of a barplot. Furthermore, I want to add these textures to the legend of the barplot.
Consider the following reproducible example:
# Create example data
data_ggp <- data.frame(category = rep(c("cat1", "cat2", "cat3"), 4),
values = c(0.664, 0.045, 0.291, 0.482, 0.029, 0.489, 0.537, 0.027, 0.436, 0.536, 0.028, 0.436),
group = c(rep("group1a", 3), rep("group1b", 3), rep("group2a", 3), rep("group2b", 3)))
# Load ggplot2
library("ggplot2")
# Draw barchart (not overlayed)
ggplot(data_ggp, aes(category, values)) +
geom_bar(stat = "identity", aes(fill = group), position = "dodge") +
scale_fill_manual(" ",
labels = c("group1a", "group1b", "group2a", "group2b"),
values = c("group1a" = "deepskyblue4", "group1b" = "darkolivegreen4",
"group2a" = "deepskyblue1", "group2b" = "darkolivegreen2"))
To this barplot, I would like to draw diagonal lines to group 2a and vertical + horizontal lines to group 2b. The legend should contain these textures, too.
The final barplot should look as follows (drawn in paint):
I found a relatively old thread on stack overflow: How to add texture to fill colors in ggplot2?
Unfortunately, this code is very complex, not automatized, and difficult to apply to different types of barplots. Furthermore, I would like to add the textures to my legend.
Question: How to add different textures to different bars of a barplot + to the legend of the barplot?