[This article was first published on Jeromy Anglim's Blog: Psychology and Statistics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This post shows how to produce a plot involving three categorical variables and one continuous variable using ggplot2 in R.

##### 1. Create Data

First, let’s load ggplot2 and create some data to work with:

library(ggplot2)

set.seed(4444)
Data <- expand.grid(group=c("Apples", "Bananas", "Carrots", "Durians",
"Eggplants"),
year=c("2000", "2001", "2002"),
Group.Weight <- data.frame(
group=c("Apples", "Bananas", "Carrots", "Durians", "Eggplants"),
group.weight=c(1,1,-1,0.5, 0))
Quality.Weight <- data.frame(
quality.weight = c(1,0.5,0,-0.5,-1))
Data <- merge(Data, Group.Weight)
Data <- merge(Data, Quality.Weight)
Data$score <- Data$group.weight + Data$quality.weight + rnorm(nrow(Data), 0, 0.2) Data$proportion.tasty <- exp(Data$score)/(1 + exp(Data$score))

##### 2. Produce Plot

And here's the code to produce the plot.

ggplot(data=Data,
aes(x=factor(year), y=proportion.tasty,
group=group,
shape=group,
color=group)) +
geom_line() +
geom_point() +
opts(title =
"Proportion Tasty by Year, Quality, and Group") +
scale_x_discrete("Year") +
scale_y_continuous("Proportion Tasty") +
facet_grid(.~quality )


And here's what it looks like: 