Joe Crozier
Joe Crozier

Reputation: 1036

GGplot, overlaying fill patterns onto fill colors

I have data like this:

Likert<-structure(list(Question = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = "satisfied_6", class = "factor"), 
    Answer = structure(c(1L, 1L, 3L, 3L, 2L, 2L, 1L, 5L, 1L, 
    2L, NA, 1L, 4L, 4L, 3L, 2L, 2L, 5L, 1L, 4L, 1L, 1L, 1L, 1L, 
    1L, 4L, 3L, 4L), .Label = c("Never", "Rarely", "Sometimes", 
    "Often", "Always"), class = "factor")), row.names = c(NA, 
-28L), class = c("tbl_df", "tbl", "data.frame"))

That I've made into a graph like this:

enter image description here

Using this code:

library(RColorBrewer)
Likert%>%filter(Question=="satisfied_6")%>%filter(!is.na(Answer))%>%group_by(Question)%>%count(Answer)%>%mutate(Percent= (n/sum(n)*100))%>%ggplot(aes(x=Percent,y=Question, fill=Answer))+geom_col()+
      theme(axis.title.y=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks.y=element_blank())+labs(title = "How frequently does impairment of your arm, shoulder, or hand negatively impact your sexual satisfaction?")+scale_x_reverse()+ scale_fill_brewer(palette = "PuBu")

We've decided that "Never" and "Rarely" can be combined to be "not impacted" and "Sometimes", "Often" and "always" can be combined to be "Impacted", and I'd love to show those two categories visually as well. My thought was to use a fill pattern overlayed on top of the colors, but I'm totally lost how to do both. My end goal was something like this:

enter image description here

Any ideas?

P.s. maybe as a comment, I'd love other suggestions for how to best visually convey that information

Upvotes: 2

Views: 981

Answers (1)

Dan Adams
Dan Adams

Reputation: 5204

This is a good use of the {ggpattern} package.

I've tried to clean up the legends for you but the code should be easy to modify from here to suit your needs.

# load packages
library(tidyverse)
library(ggpattern)
library(RColorBrewer)

Likert <- structure(list(Question = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "satisfied_6", class = "factor"), Answer = structure(c(1L, 1L, 3L, 3L, 2L, 2L, 1L, 5L, 1L, 2L, NA, 1L, 4L, 4L, 3L, 2L, 2L, 5L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 4L, 3L, 4L), .Label = c("Never", "Rarely", "Sometimes", "Often", "Always"), class = "factor")), row.names = c(NA, -28L), class = c("tbl_df", "tbl", "data.frame"))


Likert %>%
  filter(Question=="satisfied_6") %>% 
  filter(!is.na(Answer)) %>% 
  group_by(Question) %>% 
  count(Answer) %>% 
  ungroup() %>% 
  mutate(impact = c(rep("not impacted", 3), rep("impacted", 2))) %>% 
  mutate(impact = fct_rev(impact)) %>% 
  mutate(Percent= (n/sum(n)*100)) %>%
  ggplot(aes(x=Percent,y=Question, fill=Answer)) + 
  geom_col_pattern(aes(pattern_angle = impact),
                   pattern_color = NA,
                   pattern_fill = "black",
                   pattern = "stripe") +
  labs(title = "How frequently does impairment of your arm, shoulder, or hand \nnegatively impact your sexual satisfaction?") + 
  scale_x_reverse() + 
  scale_pattern_angle_manual(values = c(45, 135),
                             guide = guide_legend(title = "Impact", order = 2, override.aes = list(fill = "white", color = "black"))) +
  scale_fill_brewer(palette = "PuBu",
                    guide = guide_legend(order = 1, override.aes = list(pattern = "none")))  +
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        aspect.ratio = 0.4,
        legend.box = "horizontal") 

Created on 2022-02-04 by the reprex package (v2.0.1)

Upvotes: 2

Related Questions