Search code examples
rggplot2likert

Create Likert scale sort of graph using ggplot 2


I have a dataframe with below structure

structure(list(Set = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Set2", 
"Set1"), class = "factor"), Subset = c("Feminine", "Masculine", 
"Neutral", "Feminine", "Masculine", "Neutral"), Genderity = c(4.47, 
-3.65, 1.54, 4.13, -4.03, -0.61)), row.names = c(NA, -6L), class = "data.frame")

I want to plot a likert scale sort of plot as below output Expected Likert image output

I am struggling to replicate this in ggplot 2 (below is my code, even though it gives similar output as expected The spaces in y axis are way off because I used Genderity in both x & y argument). It was giving me error if I left y argument blank

ggplot(aes(x=Genderity, y=Genderity), data=df_2, position="stack", stat="identity")+
  geom_bar(stat="identity",aes(fill=Subset),position="dodge")+
  coord_flip()+
  geom_hline(yintercept = 0, color =c("black")) +
  scale_y_continuous(breaks=seq(-5,5,1), limits=c(-5,5))+
  facet_wrap(facets = .~Set, ncol = 2, nrow=1)

Output ggplot from my code My Output

I know my output is really close to what I want but its not the right way to do it. Please help how to do this via ggplot 2


Solution

  • We can try a workaround:

    # add a fake column
    df_2$fake <-rownames(df_2)
    
    library(ggplot2)
    
    # we add as x the fake
    ggplot(aes(x=fake, y=Genderity), data=df_2, position="stack")+
      geom_bar(stat="identity",aes(fill=Subset),position="dodge")+
      coord_flip()+
      geom_hline(yintercept = 0, color =c("black")) +
      scale_y_continuous(breaks=seq(-5,5,1), limits=c(-5,5))+
    # to avoid riffle, we use the scales="free" option
      facet_wrap(facets = .~Set, ncol = 2, nrow=1,scales="free")+
    # last, we make blank the y axis
      theme(axis.title.y=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks.y=element_blank())
    

    enter image description here

    And, if you want the order in your mock-up and if you want also the x-axis blank, you can try this:

    # the reorder option:
    ggplot(aes(x=reorder(fake,Genderity), y=Genderity), data=df_2, position="stack")+
      geom_bar(stat="identity",aes(fill=Subset),position="dodge")+
      coord_flip()+
      geom_hline(yintercept = 0, color =c("black")) +
      scale_y_continuous(breaks=seq(-5,5,1), limits=c(-5,5))+
      facet_wrap(facets = .~Set, ncol = 2, nrow=1,scales="free")+
      theme(axis.title.y=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks.y=element_blank(),
            # x-axis blank:
            axis.title.x=element_blank(),
            axis.text.x=element_blank(),
            axis.ticks.x=element_blank())
    

    enter image description here

    And:

    + ggtitle("Conveyed gender")
    

    Will give you the title.

    Edit:
    to add labels, you've to:

        p <- ggplot(aes(x=reorder(fake,Genderity), y=Genderity), data=df_2, position="stack")+
      geom_bar(stat="identity",aes(fill=Subset),position="dodge")+
      coord_flip()+
      geom_hline(yintercept = 0, color =c("black")) +
      scale_y_continuous(breaks=seq(-5,5,1), limits=c(-5,5))+
      facet_wrap(facets = .~Set, ncol = 2, nrow=1,scales="free")+
      theme(axis.title.y=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks.y=element_blank(),
            # x-axis blank:
            axis.title.x=element_blank(),
            axis.text.x=element_blank(),
            axis.ticks.x=element_blank())
    p <- p + ggtitle("Conveyed gender") + geom_text(aes(label=Genderity), hjust=1)
    p
    

    enter image description here

    Or you can use also:

    + geom_text(aes(label = Genderity), position = position_dodge(0.9))