How to get plotmath-labels in a faceted boxplot?

51 views
Skip to first unread message

Marius Hofert

unread,
Nov 21, 2011, 8:53:22 AM11/21/11
to ggplot2
Hi,

How can I get plotmath-labels instead of g1,...,g5 in the minimal
example below?

Cheers,

Marius


require(ggplot2)
require(reshape2)
require(plyr)

## parameters
p1 <- c(5, 20, 100)
p2 <- c("f1", "f2", "f3", "f4", "f5")
p3 <- c("g1","g2","g3","g4","g5")
N <- 1000

## lengths
l1 <- length(p1)
l2 <- length(p2)
l3 <- length(p3)

## build result array containing the measurements
arr <- array(rep(NA, l1*l2*l3*N), dim=c(l1, l2, l3, N),
dimnames=list(
p1=p1,
p2=p2,
p3=p3,
N=1:N))
for(i in 1:l1){
for(j in 1:l2){
for(k in 1:l3){
arr[i,j,k,] <- i+j+k+runif(N)
}
}
}

## create molten data
mdf <- melt(arr, formula = . ~ p1 + p2 + p3 + N) # create molten data
frame

## plot
p3.labs <- lapply(1:l3, function(i) bquote(alpha==.(i)))
ggplot(mdf, aes(x=p3.labs, y=value), labeller=label_parsed) +
geom_boxplot() + facet_grid(p2 ~ p1, scales = "free_y")

Brandon Hurr

unread,
Nov 21, 2011, 9:17:04 AM11/21/11
to Marius Hofert, ggplot2
Does this work for you?

ggplot(mdf, aes(x=p3, y=value), labeller=label_parsed) +
geom_boxplot() + facet_grid(p2 ~ p1, scales = "free_y")+
scale_x_discrete(labels = c('g1'= expression(alpha == 1),'g2'= expression(alpha == 2),'g3'= expression(alpha == 3),'g4'= expression(alpha == 4),'g5'= expression(alpha == 5)))

I was trying some other things and they might have got jumbled.

B

Marius Hofert

unread,
Nov 21, 2011, 4:24:35 PM11/21/11
to Brandon Hurr, ggplot2
Dear Brandon,

thanks a lot, that was precisely what I was looking for.

Cheers,

Marius

Reply all
Reply to author
Forward
0 new messages