2

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

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
Joe Crozier
  • 944
  • 8
  • 20
  • What about just putting "impacted" vs "not impacted" as two facets? – Nova Feb 04 '22 at 16:00
  • The way I'm thinking about that, it would split it into two "graphs" (each facet) and you wouldn't see for instance how the percentage "always" relates to the percentage of "rarely" if that makes sense. I'd love to keep the impacted/not impacted on the same graph. I was thinking maybe lines/annotations above it all? – Joe Crozier Feb 04 '22 at 16:03
  • 1
    For the pattern, you can try using `geom_col_pattern()`, which is from the `ggpattern` [library](https://coolbutuseless.github.io/package/ggpattern/index.html). I see what you mean in terms of facets. Alternatively, I recommend to look into the `ggh4x` [library](https://github.com/teunbrand/ggh4x) that has some options which could prove useful here to group the values together. – chemdork123 Feb 04 '22 at 16:14

1 Answers1

2

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)

Dan Adams
  • 4,971
  • 9
  • 28
  • I'm getting "package ggpattern is not available for this version of R" (I'm 4.1.0). Is it still supported? Or is it something on my end? – Joe Crozier Feb 04 '22 at 16:48
  • 1
    Install directly from github: `remotes::install_github("coolbutuseless/ggpattern")` – Dan Adams Feb 04 '22 at 16:51