playing with ggplot 2

I have been playing around with ggplot2 a lot recently.  One thing I like about ggplot2 is the ability to condense a lot of information into graphical summaries.  Sometimes, I need a quick and easy way to look at survey response frequencies across different groups.  I came across this posting and wanted to try it myself on some of my own data.  Here is what I did:

library(ggplot2)
data_qa1 <- read.csv("data_qa1.csv")
p <- ggplot(data_qa1)
p1 <- p + geom_bar(aes(group, adj.freq, colour=group),
                   stat="identity")
p2 <- p1 + geom_bar(aes(group, adj.freq, fill=item),
                    stat="identity", position="dodge")
p2

And this is what came out:

I am pleased with it.  Then following the directions in the ggplot2 book, I was able to facet several item sets at the same time by creating a viewport function:

layout <- grid.layout( nrow = 2, ncol = 2,
widths = unit (c(1,1), c("null", "null")),
heights = unit (c(1,2), c("null", "null")))
vplayout <- function (...) {
  grid.newpage()
  pushViewport(viewport(layout = layout))
}
subplot <- function(x, y)
viewport(layout.pos.row=x, layout.pos.col=y)
vplayout()

data_qa1 <- read.csv("data_qa1.csv")
data_qa2 <- read.csv("data_qa2.csv")
data_qa3 <- read.csv("data_qa3.csv")
data_qa4 <- read.csv("data_qa4.csv")  

p <- ggplot(data_qa1)
p1 <- p + geom_bar(aes(group, adj.freq, colour=group),
                   stat="identity")
p2 <- p1 + geom_bar(aes(group, adj.freq, fill=item),
                    stat="identity", position="dodge")
print(p2,vp=subplot(1, 1))

p <- ggplot(data_qa2)
p1 <- p + geom_bar(aes(group, adj.freq, colour=group),
                   stat="identity")
p2 <- p1 + geom_bar(aes(group, adj.freq, fill=item),
                    stat="identity", position="dodge")
print(p2,vp=subplot(1, 2))

p <- ggplot(data_qa3)
p1 <- p + geom_bar(aes(group, adj.freq, colour=group),
                   stat="identity")
p2 <- p1 + geom_bar(aes(group, adj.freq, fill=item),
                    stat="identity", position="dodge")
print(p2,vp=subplot(2, 1))

p <- ggplot(data_qa4)
p1 <- p + geom_bar(aes(group, adj.freq, colour=group),
                   stat="identity")
p2 <- p1 + geom_bar(aes(group, adj.freq, fill=item),
                    stat="identity", position="dodge")
print(p2,vp=subplot(2, 2))

Which gave me this:

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>