我有以下表格的调查数据数据框:
category shift difficulty importance frequency dsmImportance supervisor
1 Monitoring Day 3 1 1 3 Debra Smith
2 Monitoring Day 2 1 1 3 Debra Smith
3 Paperwork Night 3 1 1 3 Mark Hobbs
4 Operations Day 1 1 1 2 Ryan Jones
5 Rostering Night 1 1 1 1 Mark Hobbs
数据是对工作班次期间执行的任务的调查,根据每个任务的难度,重要性等分配1-3的等级。
我想要做的是绘制任务评级的直方图数组,其中difficulty
,importance
,frequency
和dsmImportance
用于数组列和行category
。到目前为止,我的方法是为每个评分类型(difficulty
,importance
等)创建单个列,并使用category
进行分组,然后使用grid_layout()
将列分组在一起。您可以看到结果here。 (不幸的是,我被阻止直接链接到图像,直到我成为会员更长时间。)它有效,但它并不是非常漂亮。
如何使用ggplot2
的分面功能完全创建数组?我是R的新手(和堆栈溢出),但我很确定我可以'使用当前所处形式的数据执行此操作。我认为我必须将数据融合并将其转换为不同的形式,但我不知道该形式应该是什么。
library(ggplot2)
library(gridExtra)
walkaday.dirty = read.csv("~/Documents/walkaday.csv", header = TRUE, sep = ",", fill = TRUE, blank.lines.skip = TRUE)
walkaday = na.omit(walkaday.dirty)
// Order category levels by task frequency
category.levels = names(sort(table(walkaday$category), decreasing = TRUE))
walkaday$category = factor(walkaday$category, levels = category.levels)
difficulty = ggplot(walkaday, aes(factor(difficulty, c("3", "2", "1")), fill = difficulty)) + geom_bar() + coord_flip() + xlab("") + ylab("") + opts(legend.position = "none")
difficulty = difficulty + facet_grid(category ~ .) + opts(strip.text.y = theme_blank())
importance = ggplot(walkaday, aes(factor(importance, c("3", "2", "1")), fill = importance)) + geom_bar() + coord_flip() + xlab("") + ylab("") + opts(legend.position = "none", axis.text.y = theme_blank(), axis.ticks = theme_blank())
importance = importance + facet_grid(category ~ .) + opts(strip.text.y = theme_blank())
frequency = ggplot(walkaday, aes(factor(frequency, c("3", "2", "1")), fill = frequency)) + geom_bar() + coord_flip() + xlab("") + ylab("") + opts(legend.position = "none", axis.text.y = theme_blank(), axis.ticks = theme_blank())
frequency = frequency + facet_grid(category ~ .) + opts(strip.text.y = theme_blank())
dsmImportance = ggplot(walkaday, aes(factor(dsmImportance, c("3", "2", "1")), fill = dsmImportance)) + geom_bar() + coord_flip() + xlab("") + ylab("") + opts(legend.position = "none", axis.text.y = theme_blank(), axis.ticks = theme_blank())
dsmImportance = dsmImportance + facet_grid(category ~ .) + opts(strip.text.y = theme_text(angle = 0))
pushViewport(viewport(layout = grid.layout(1, 4, widths = c(1,1,1,1.7))))
print(difficulty + opts(title = "Task difficulty"), vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(importance + opts(title = "Task importance"), vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
print(frequency + opts(title = "Task frequency"), vp = viewport(layout.pos.row = 1, layout.pos.col = 3))
print(dsmImportance + opts(title = "DSM importance"), vp = viewport(layout.pos.row = 1, layout.pos.col = 4))
可以找到数据集here。
答案 0 :(得分:0)
melt
将您的数据转换为长格式,以便评级类型显示为单独的变量:
walkaday <- read.csv("http://dl.dropbox.com/u/7046039/walkaday.csv")
walkaday.long <- melt(walkaday,id.vars=c(1,2,7))
qplot(factor(value,c("3","2","1")),data=walkaday.long,geom="bar")+facet_grid(.~variable)
请注意,新变量的名称为variable
,值为value
。