简体   繁体   中英

Plotting data frame column values as facet columns

I have a data frame of survey data in the following form:

    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

The data is a survey of tasks performed during a work shift, with a rating of 1-3 assigned according to each tasks' difficulty, importance, etc.

What I'd like to do is plot an array of histograms of the task ratings, with difficulty , importance , frequency and dsmImportance for the array columns and category for the rows. My approach so far has been to create single columns for each rating type ( difficulty , importance etc.) faceted with category , and then group the columns together using grid_layout() . You can see the result here . (Unfortunately I'm prevented from linking directly to an image until I've been a member for longer.) It works but it's not terribly pretty.

How do I go about creating the array entirely using ggplot2 's faceting function? I'm new to R (and stack overflow) but I'm fairly sure I can't do this with the data in the form that it's currently in. I presume I have to melt the data and cast it into a different form, but I don't know what that form should be.

Code

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 chart

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 chart

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 chart

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())

DSM Importance chart

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))

Combine charts

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))

Data

The dataset can be found here .

melt your data into long form so that the rating type appears as a separate variable:

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)

Note the name of the new variable is variable and the values are value .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM