Fbj9506
Fbj9506

Reputation: 251

How modify stacked bar chart in ggplot2 so it is diverging

My data (from a likert scale question) looks like this:

head(dat)
            Consideration Importance2           Importance  Percent Count
1         Aesthetic value           1 Not at all important 0.046875     3
2         Aesthetic value           2 Of little importance 0.109375     7
3         Aesthetic value           3 Moderately important 0.250000    16

dput(head(dat,6))
structure(list(Consideration = structure(c(2L, 2L, 2L, 2L, 2L, 
12L), .Label = c("", "Aesthetic value", "Affordability/cost-efficiency", 
"Climate change reduction", "Eco-sourcing", "Ecosystem services provision", 
"Erosion mitigation", "Habitat for native wildlife", "Habitat/species conservation", 
"Human use values", "Increasing biodiversity", "Planting native species", 
"Restoring ecosystem function", "Restoring to a historical state"
), class = "factor"), Importance2 = c(1L, 2L, 3L, 4L, 5L, 1L), 
    Importance = structure(c(4L, 5L, 3L, 2L, 6L, 4L), .Label = c("", 
    "Important", "Moderately important", "Not at all important", 
    "Of little importance", "Very Important"), class = "factor"), 
    Percent = c(0.046875, 0.109375, 0.25, 0.375, 0.234375, 0), 
    Count = c(3L, 7L, 16L, 24L, 15L, 0L), percentage = c(5L, 
    11L, 25L, 38L, 23L, 0L)), row.names = c(NA, 6L), class = "data.frame")

I've plotted the results using a stacked bar chart. I would like to know how to modify this so it's a diverging stacked bar chart such as the example shown below, with the Importance2 level 3 (moderately important) as the centre. example

I know there is a package called likert that can be used for this, but I think my data is not in the correct format.

The code for my existing plot is:

ggplot(dat, aes(x = Consideration, y = Percent, fill = forcats::fct_rev(Importance2))) +
  geom_bar(position="fill", stat = "identity", color = "black", size = 0.2, width = 0.8) +
  aes(stringr::str_wrap(dat$Consideration, 34), dat$Percent) +
  coord_flip() +
  labs(y = "Percentage of respondents (%)") +
  scale_y_continuous(breaks=c(0, 0.25, 0.50, 0.75, 1), labels=c("0", "25", "50", "75", "100")) +
  theme(axis.title.y=element_blank(), panel.background = NULL, axis.text.y = element_text(size=8), legend.title = element_text(size=8), legend.text = element_text(size = 6)) +
  scale_fill_manual(name="Scale", breaks=c("1", "2", "3", "4", "5"), labels=c("Not at all important", "Of little importance", "Moderately important","Important", "Very important"), values=col3)

Current plot

Upvotes: 1

Views: 5583

Answers (1)

s__
s__

Reputation: 9485

I've tried a couple of solution, but I think that the simplest one is to convert your data for the likert() function, and it's quite simple:

library(tidyr)
# you need the data in the wide format
data_l <- spread(dat[,c(1,3,4)], key = Importance, value = Percent)
# now add colnames
row.names(data_l) <- data_l$Consideration
# remove the useless column
data_l <- data_l[,-1]

Now you can use:

library(HH)
likert(data_l , horizontal=TRUE,aspect=1.5,
       main="Here the plot",
       auto.key=list(space="right", columns=1,
                     reverse=TRUE, padding.text=2),
       sub="Here some words")

enter image description here

You can tweak ggplot to do this, but in that case you do not center by the center of the class you want, but by the "edge" of it.

Upvotes: 2

Related Questions