Search code examples
rggplot2geom-bar

Percents in stacked bar chart


I'm trying to use percents as labels in a stacked bar chart, but when I convert the column to percents instead of decimals the bar chart automatically becomes vertical and no longer stacked. I would appreciate some helping making these labels percents instead of decimals.

Thanks.

Here is my sample code:

Form <- c(rep("Overall" , 4) , rep("x" , 4) , rep("y" , 4) , rep("z" , 4) )
Ease <- rep(c("Very Easy", "Somewhat Easy", "Somewhat Difficult", "Very Difficult") , 1)
Mean <- c(.28, .5, .19, .44, .33, .48, .15,.4, .32, .51, .15, .2, .30, .50, .16, .4)
data <- data.frame(Form,Ease,Mean)

    ggplot(data, aes(x=Mean, y=Form, fill=Ease, label = Mean))+  
    geom_bar(position = "stack", stat = "identity", width = .4,
               size=.2) +
      geom_text(aes(x=Mean, y=Form, label = Mean),
                position = position_stack(vjust = .5),
                size = 4, color = "white", fontface = "bold")+
      theme_fivethirtyeight()+
      theme( legend.position = "bottom",
             legend.key.width = unit(.7, "cm"),
             legend.title = element_text(family="serif", size=8, color = "black", face = "bold"),
             legend.text = element_text(family="serif", size=8, color = "black", face = "bold"),
             legend.key.size = unit(.1, "cm"),
             axis.line = element_line(size = 1),
             axis.title = element_text(family="serif", size=18, color = "black", face = "bold"),
             axis.text = element_blank(),
             axis.ticks = element_blank(),
             plot.background = element_blank(),
             panel.background = element_blank(),
             plot.title = element_text(hjust = 0.5, family="serif", size=22, color = "black", face = "bold"),
             strip.text.x = element_text(family="serif", size=12, color = "black", face = "bold")) +
      scale_fill_brewer(palette = "Set2")+
      xlab("")+
      ylab("")

Solution

  • Using this

    ggplot(data, aes(x=Mean, y=Form, fill=Ease, label = Mean))+  
      geom_bar(position = "stack", stat = "identity", width = .4,
               size=.2) +
      geom_text(aes(x=Mean, y=Form, label = scales::percent(Mean)),
                position = position_stack(vjust = .5),
                size = 4, color = "white", fontface = "bold")+
      
      #theme_fivethirtyeight()+
      theme( legend.position = "bottom",
             legend.key.width = unit(.7, "cm"),
             legend.title = element_text(family="serif", size=12, color = "black", face = "bold"),
             legend.text = element_text(family="serif", size=8, color = "black", face = "bold"),
             legend.key.size = unit(.1, "cm"),
             axis.line = element_line(size = 1),
             axis.title = element_text(family="serif", size=18, color = "black", face = "bold"),
             axis.text = element_blank(),
             axis.ticks = element_blank(),
             plot.background = element_blank(),
             panel.background = element_blank(),
             plot.title = element_text(hjust = 0.5, family="serif", size=22, color = "black", face = "bold"),
             strip.text.x = element_text(family="serif", size=12, color = "black", face = "bold")) +
      scale_fill_brewer(palette = "Set2")+
      xlab("")+
      ylab("")
    

    we get

    output

    It is no different than your original output

    output