堆积条形图中的百分比

时间:2021-03-02 03:24:03

标签: r ggplot2 geom-bar

我试图在堆积条形图中使用百分比作为标签,但是当我将列转换为百分比而不是小数时,条形图会自动变为垂直且不再堆积。如果您能帮助我将这些标签设为百分比而不是小数,我将不胜感激。

谢谢。

这是我的示例代码:

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("")

1 个答案:

答案 0 :(得分:0)

使用这个

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("")

我们得到

output

它与您的原始输出没有什么不同

output