How to plot three categorical variables and one continuous variable using ggplot2

May 3, 2012

(This article was first published on Jeromy Anglim's Blog: Psychology and Statistics, and kindly contributed to R-bloggers)

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

The following code is also available as a gist on github.

1. Create Data

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


Data <- expand.grid(group=c("Apples", "Bananas", "Carrots", "Durians",
year=c("2000", "2001", "2002"),
quality=c("Grade A", "Grade B", "Grade C", "Grade D",
"Grade E"))
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=c("Grade A", "Grade B", "Grade C", "Grade D", "Grade E"),
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.

aes(x=factor(year), y=proportion.tasty,
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:

three categorical variables ggplot2

To leave a comment for the author, please follow the link and comment on their blog: Jeromy Anglim's Blog: Psychology and Statistics. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)