Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: Add Different Textures to Colored Barplot and Legend [duplicate]

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"))

enter image description here

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):

enter image description here

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?

like image 430
Joachim Schork Avatar asked Nov 06 '22 22:11

Joachim Schork


1 Answers

I was able to solve this using ggpattern

# remotes::install_github("coolbutuseless/ggpattern")
library(ggpattern)
library(ggplot2)

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)))

ggplot(data_ggp, aes(category, values)) + 
  geom_bar_pattern(stat = "identity", 
                   pattern = c("none", "none", "none", # 1st col
                               "none", "none", "none", # 2nd col
                               "stripe", "stripe", "stripe", # 3rd col
                               "crosshatch", "crosshatch", "crosshatch" # 4th col 
                               ),
                   pattern_angle = c(rep(0, 6), rep(45, 3), rep(0, 3)),
                   pattern_density = .1,
                   pattern_spacing = .04,
                   pattern_fill = 'black',
                   aes(fill = group), 
                   position = "dodge") +
  scale_fill_manual(" ", 
                    labels = c("group1a", "group1b", "group2a", "group2b"),
                    values = c("group1a" = "deepskyblue4", "group1b" = "darkolivegreen4", 
                               "group2a" = "deepskyblue1", "group2b" = "darkolivegreen2")) +
  guides(fill = guide_legend(override.aes = 
                               list(
                                 pattern = c("none", "none", "stripe", "crosshatch"),
                                 pattern_spacing = .01,
                                 pattern_angle = c(0, 0, 45, 0)
                                 )
                             ))

Created on 2021-01-13 by the reprex package (v0.3.0)

like image 80
John-Henry Avatar answered Nov 15 '22 12:11

John-Henry