【问题标题】:How to make a stacking bar using ggplot?如何使用ggplot制作堆叠条?
【发布时间】:2020-03-29 21:47:36
【问题描述】:

我有这个数据集。我正在尝试使用 ggplot 为这些数据制作一个具有比例的堆叠条形图:

我不太确定如何首先将其操作到表格中!我知道,我刚开始学习 R,两周前,我有点卡住了。我之前做过类似的图表。我把它贴在这里。

【问题讨论】:

  • 嗨,@AAAA。欢迎来到 StackOverflow!请提供一些示例代码,以便我们查看您的尝试并帮助纠正任何错误。

标签: ggplot2 graph charts ggplotly


【解决方案1】:

我不确定你的问题是否正确,但我会尽力回答。我知道这是您在 Stack Overflow 上的第一个问题,所以我建议您在下一个问题上发布 minimal reproducible example

1) “我不太确定如何首先将其操作到表格中!”

将数据复制到 excel 文件中,将其保存为 csv 并使用基本 R 命令导入到 R 中。

df <- read.csv('your_data.csv')

2) "做一个有比例的堆积条形图"

您的问题与this question 中提到的问题非常相似。一定要检查一下,但我已经修改了下面的代码,看看它是否有效。

    library(ggplot2)
    library(dplyr)
    library(tidyr)

    df <- read.csv('your_data.csv')

    # Add an id variable for the filled regions and reshape
    dfm <- df %>% 
      mutate(Domain = factor(row_number()) %>% 
      gather(variable, value, -Domain)

    ggplot(dfm, aes(x = variable, y = value, fill = Domain)) + 
        geom_bar(position = "fill",stat = "identity") +
        # or:
        # geom_bar(position = position_fill(), stat = "identity"
    scale_y_continuous(labels = scales::percent_format())

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 2022-12-31
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2021-11-16
    • 2018-03-17
    • 1970-01-01
    相关资源
    最近更新 更多