【问题标题】:How to create a stacked bar chart from summarized data in ggplot2如何从 ggplot2 中的汇总数据创建堆积条形图
【发布时间】:2012-08-11 22:12:16
【问题描述】:

我正在尝试使用 ggplot 2 创建一个堆叠条形图。我的宽格式数据看起来像这样。每个单元格中的数字是响应的频率。

activity                         yes    no  dontknow
Social events                     27    3   3
Academic skills workshops         23    5   8
Summer research                   22    7   7
Research fellowship               20    6   9
Travel grants                     18    8   7
Resume preparation                17    4   12
RAs                               14    11  8
Faculty preparation               13    8   11
Job interview skills              11    9   12
Preparation of manuscripts        10    8   14
Courses in other campuses          5    11  15
Teaching fellowships               4    14  16
TAs                                3    15  15
Access to labs in other campuses   3    11  18
Interdisciplinary research         2    11  18
Interdepartamental projects        1    12  19

我使用 reshape2 和

融化了这张桌子
 melted.data(wide.data,id.vars=c("activity"),measure.vars=c("yes","no","dontknow"),variable.name="haveused",value.name="responses")

这是我所能得到的。我想创建一个堆积条形图,其中 x 轴为活动,y 轴为响应频率,每个条形图显示是、否和不知道的分布

我试过了

ggplot(melted.data,aes(x=activity,y=responses))+geom_bar(aes(fill=haveused))

但恐怕这不是正确的解决方案

非常感谢任何帮助。

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    您还没有说您的解决方案有什么不妥之处。但是一些可以被解释为问题的问题,以及每个问题的一个可能的解决方案是:

    • x 轴刻度线标签相互碰撞。解决方案 - 旋转刻度线标签;
    • 标签(及其对应的条形)出现的顺序与原始数据框中的顺序不同。解决方案 - 重新排列因素“活动”的水平;
    • 要将文本定位在条内,请将 position_stack 中的 vjust 参数设置为 0.5

    以下可能是一个开始。

        # Load required packages
    library(ggplot2)
    library(reshape2)
    
        # Read in data
    df = read.table(text = "
    activity                         yes    no  dontknow
    Social.events                     27    3   3
    Academic.skills.workshops         23    5   8
    Summer.research                   22    7   7
    Research.fellowship               20    6   9
    Travel.grants                     18    8   7
    Resume.preparation                17    4   12
    RAs                               14    11  8
    Faculty.preparation               13    8   11
    Job.interview.skills              11    9   12
    Preparation.of.manuscripts        10    8   14
    Courses.in.other.campuses          5    11  15
    Teaching.fellowships               4    14  16
    TAs                                3    15  15
    Access.to.labs.in.other.campuses   3    11  18
    Interdisciplinay.research          2    11  18
    Interdepartamental.projects        1    12  19", header = TRUE, sep = "")
    
        # Melt the data frame
    dfm = melt(df, id.vars=c("activity"), measure.vars=c("yes","no","dontknow"),
        variable.name="haveused", value.name="responses")
    
        # Reorder the levels of activity
    dfm$activity = factor(dfm$activity, levels = df$activity)
    
        # Draw the plot
    ggplot(dfm, aes(x = activity, y = responses, group = haveused)) + 
    geom_col(aes(fill=haveused)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) +
    geom_text(aes(label = responses), position = position_stack(vjust = .5), size = 3)  # labels inside the bar segments
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多