【问题标题】:Percents in stacked bar chart堆积条形图中的百分比
【发布时间】:2021-05-31 14:39:33
【问题描述】:

我正在尝试将百分比用作堆叠条形图中的标签,但是当我将列转换为百分比而不是小数时,条形图会自动变为垂直且不再堆叠。我会很感激一些帮助制作这些标签的百分比而不是小数。

谢谢。

这是我的示例代码:

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

【问题讨论】:

    标签: r ggplot2 geom-bar


    【解决方案1】:

    使用这个

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

    我们得到

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

    【讨论】:

    • 谢谢。问题是我需要标签中的百分号。因此,例如,如果我这样做:data$Mean= percent(data$Mean, accuracy = 1)。一切都错了。
    • @JohnD'Attoma,请检查更新后的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多