【问题标题】:Barchart percentage subgroups [duplicate]条形图百分比子组 [重复]
【发布时间】:2020-02-09 18:17:02
【问题描述】:

我有一个来自 csv 的包含 15 个指标(列)的数据集。 1 个指标称为Cancer

这是数据集中列的样子

Cancer:  yes no yes no

我只想有一个条形图(堆叠和正常)显示癌症是和否的百分比

【问题讨论】:

    标签: r


    【解决方案1】:

    这应该使用ggplot2 完成所需的堆叠图。

    library(ggplot2)
    
    ggplot(df,aes(x="", fill = Cancer))+
      #Do the bar plot scaled to 1
      geom_bar(position = "fill") +
      #Change the y axis labels as percent
      scale_y_continuous(labels = scales::percent)
    

    【讨论】:

    • 谢谢它的工作。你也可以用是/否癌症的百分比制作一个简单的表格吗?但我正在从不同的子组(过滤器:50-54 岁,agebirthfirstchild
    • 当我使用不同的变量与子类别时,如何做同样的事情?在它们中,例如如果我的性别包含男性女性,疾病包含多种亚型等
    【解决方案2】:

    1.创建minimal reproducible example

    df <- data.frame(Cancer = c("yes", "yes", rep("no", 8)))
    

    2.使用ggplot2scales的解决方案

    如果您只需要情节的摘要,我们可以直接使用 ggplot:

    library(ggplot2)
    library(scales)
    ggplot(df, aes(x = Cancer)) +  
      geom_bar(aes(y = (..count..)/sum(..count..))) + 
      scale_y_continuous(labels=scales::percent) +
      labs(x="Cancer", y="Relative frequency [%]")
    

    【讨论】:

    • 这个也可以,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多