Note that there are some explanatory texts on larger screens.

plurals
  1. POPlotting data frame column values as facet columns
    primarykey
    data
    text
    <p>I have a data frame of survey data in the following form:</p> <pre><code> 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 </code></pre> <p>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. </p> <p>What I'd like to do is plot an array of histograms of the task ratings, with <code>difficulty</code>, <code>importance</code>, <code>frequency</code> and <code>dsmImportance</code> for the array columns and <code>category</code> for the rows. My approach so far has been to create single columns for each rating type (<code>difficulty</code>, <code>importance</code> etc.) faceted with <code>category</code>, and then group the columns together using <code>grid_layout()</code>. You can see the result <a href="http://imgur.com/4OSgO" rel="nofollow">here</a>. (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.</p> <p><strong>How do I go about creating the array entirely using <code>ggplot2</code>'s faceting function?</strong> 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.</p> <h2>Code</h2> <pre><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) </code></pre> <h3>Difficulty chart</h3> <pre><code>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()) </code></pre> <h3>Importance chart</h3> <pre><code>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()) </code></pre> <h3>Frequency chart</h3> <pre><code>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()) </code></pre> <h3>DSM Importance chart</h3> <pre><code>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)) </code></pre> <h3>Combine charts</h3> <pre><code>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)) </code></pre> <h2>Data</h2> <p>The dataset can be found <a href="http://dl.dropbox.com/u/7046039/walkaday.csv" rel="nofollow">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload