使用ggplot2分组的条形图 [英] Grouped bar chart using ggplot2

查看:61
本文介绍了使用ggplot2分组的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据结构如下:

I have some data structured in the same way as the following:

structure(list(respectfromsuperior = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, NA, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("agree", 
"disagree"), class = "factor"), respectideserve = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L), .Label = c("agree", 
"disagree"), class = "factor"), undesirablechange = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, NA, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, NA, 1L, 2L, 1L, 2L, 2L, 2L), .Label = c("agree", 
"disagree"), class = "factor"), jobsecuritypoor = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("agree", 
"disagree"), class = "factor"), promotionprospectsadequate = structure(c(2L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 
2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("agree", 
"disagree"), class = "factor"), salaryadequate = structure(c(2L, 
1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 
2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("agree", 
"disagree"), class = "factor"), branch = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Edinburgh", 
"Head Office", "Manchester"), class = "factor")), .Names = c("respectfromsuperior", 
"respectideserve", "undesirablechange", "jobsecuritypoor", "promotionprospectsadequate", 
"salaryadequate", "branch"), class = "data.frame", row.names = c(1L, 
2L, 4L, 6L, 10L, 11L, 13L, 15L, 16L, 17L, 19L, 20L, 22L, 23L, 
25L, 27L, 29L, 30L, 32L, 33L, 34L, 35L, 39L, 40L, 41L, 42L, 43L, 
44L, 45L))

我想使用ggplot 2绘制具有以下功能的条形图:

I would like to use ggplot 2 to plot a bar graph with the following features:

  1. 条形图表示同意的受访者百分比
    数据第2:6列中的语句(不同意,未绘制).百分比计算为
    分支机构成员所占百分比(不是占总数的百分比受访者)
  2. 在x轴上按分支分组的钢筋
  3. 问题(第2栏:第6列)用作填充"参数

我尝试使用下面的代码,但无法解决:

I've tried playing around with the code below but not able to work it out:

data.r <- melt(rewitemsbr, id.vars='branch')
ggplot(data=data.r, aes(x=value, fill=variable)) +
geom_bar(stat="count", position=position_dodge())

这是我想出的最好的方法:

this is the best I've come up with:

非常感谢您的帮助.

推荐答案

您可以尝试以下方法.

# get the stats using aggregate
res <- aggregate(d[,1:6], list(d$branch), function(x) sum(x=="agree", na.rm = T)/length(x))
res
  Group.1   respectfromsuperior respectideserve undesirablechange jobsecuritypoor promotionprospectsadequate salaryadequate
1 Edinburgh                 1.0       0.8888889         0.1111111             0.0                  0.6666667      0.4444444
2 Head Office               0.7       0.3000000         0.4000000             0.2                  0.2000000      0.0000000
3 Manchester                0.8       0.8000000         0.2000000             0.1                  0.6000000      0.2000000
# to long format
library(reshape2)
res_long <- melt(res, id.vars='Group.1')
# plot
ggplot(data=res_long, aes(x=Group.1, y=value, fill=variable)) +
  geom_bar(stat="identity", position=position_dodge())

这篇关于使用ggplot2分组的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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