2012-04-28 5 views
4

次のプロットの凡例のラベルに式(plotmath)を挿入するにはどうすればよいですか?凡例のラベルに式(plotmath)を入れる

私はHow to use Greek symbols in ggplot2?とそのリンクを認識していますが、scale_..._manual機能を使用するたびに、私は2番目の凡例を取得します(下記参照)。

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

## parameters 
d <- c(2, 5, 10, 20, 50, 100) 
tau <- c("t1", "t2", "t3") 
fam <- c("f1", "f2", "f3", "f4", "f5") 
meth <- c("m1", "m2", "m3", "m4") 

## lengths 
nd <- length(d) 
ntau <- length(tau) 
nfam <- length(fam) 
nmeth <- length(meth) 

## build result array containing the measurements 
arr <- array(rep(NA, nd*ntau*nfam*nmeth), dim=c(nd, ntau, nfam, nmeth), 
      dimnames=list(d=d, tau=tau, fam=fam, meth=meth)) 
for(i in 1:nd){ 
    for(j in 1:ntau){ 
     for(k in 1:nfam){ 
      for(l in 1:nmeth){ 
       arr[i,j,k,l] <- i+j+k+l+runif(1) 
      } 
     } 
    } 
} 

## create molten data 
mdf <- reshape2:::melt.array(arr, formula = . ~ d + tau + fam + meth) # create molten data frame 
mdf$tau. <- factor(mdf$tau, levels=tau, labels=paste("tau==", tau, sep="")) # expression for tau 
mdf$fam. <- factor(mdf$fam, levels=fam, labels=paste("alpha==", fam, sep="")) # expression for family 
meth.labs <- lapply(1:nmeth, function(i) bquote(gamma==.(i))) # expression for methods 

## plot 
ggplot(mdf, aes(x=d, y=value, shape=meth, linetype=meth)) + geom_line() + 
    geom_point() + facet_grid(fam. ~ tau., scales="free_y", labeller=label_parsed) + 
    ## scale_linetype_manual(values=1:4, breaks=meth, labels=meth.labs) + # problem: adds another legend 
    scale_x_continuous(trans="log10", breaks=d, labels=d) + 
    scale_y_continuous(trans="log10") 

答えて

7

私は表現を持つ単一の凡例を取得両方scale_*_manualの機能を使用する場合:

ggplot(mdf, aes(x=d, y=value, shape=meth, linetype=meth)) + geom_line() + 
    geom_point() + facet_grid(fam. ~ tau., scales="free_y", labeller=label_parsed) + 
    ## scale_linetype_manual(values=1:4, breaks=meth, labels=meth.labs) + # problem: adds another legend 
    scale_x_continuous(trans="log10", breaks=d, labels=d) + 
    scale_y_continuous(trans="log10") + 
    scale_linetype_manual(breaks = c('m1','m2','m3','m4'),values = 1:4,labels = meth.labs) + 
    scale_shape_manual(breaks = c('m1','m2','m3','m4'),values = 1:4,labels = meth.labs) 

enter image description here

+0

おかげで、joranは、素晴らしいですね!私はそれを私の元の例に入れ、それがまだ動作するかどうかを見てみましょう:-) –

関連する問題