如何从ggplot2中的汇总数据创建堆叠的条形图 [英] How to create a stacked bar chart from summarized data in ggplot2

查看:715
本文介绍了如何从ggplot2中的汇总数据创建堆叠的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a stacked bar graph using ggplot 2. My data in its wide form, looks like this. The numbers in each cell are the frequency of responses.

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和

I melted this table using reshape2 and

 melted.data(wide.data,id.vars=c("activity"),measure.vars=c("yes","no","dontknow"),variable.name="haveused",value.name="responses")

据我所知.我想创建一个堆积的条形图,在x轴上有活动,在y轴上有响应频率,并且每个条形都显示yes,nos和dontknows的分布情况

That's as far as I can get. I want to create a stacked bar chart with activities on the x axis, frequency of responses in the y axis, and each bar showing the distribution of the yes, nos and dontknows

我尝试过

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

但是我担心这不是正确的解决方案

but I'm afraid that's not the right solution

我们非常感谢您的帮助.

Any help is much appreciated.

推荐答案

您还没有说明解决方案不正确的地方.但是,可以解释为问题的一些问题,以及每个问题的一种可能解决方案是:

You haven't said what it is that's not right about your solution. But some issues that could be construed as problems, and one possible solution for each, are:

  • x轴刻度线标签相互衔接.解决方案-旋转刻度线标签;
  • 标签(及其对应的条形)的显示顺序与原始数据框中的顺序不同.解决方案-重新排序活动"因子的水平;
  • 要将文本放置在条形图内,请将position_stack中的vjust参数设置为0.5
  • The x axis tick mark labels run into each other. SOLUTION - rotate the tick mark labels;
  • The order in which the labels (and their corresponding bars) appear are not the same as the order in the original dataframe. SOLUTION - reorder the levels of the factor 'activity';
  • To position text inside the bars set the vjust parameter in position_stack to 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

这篇关于如何从ggplot2中的汇总数据创建堆叠的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆