2013-11-15 30 views
5

変数(ここではベクトル要素 "型")とn軸ラベル内に上付き文字(ここではm^2)を含む単位を一緒に使用したいと思います。ggplot2で軸ラベルに同時に上付き文字と変数を使用する方法

data <- list(houses = data.frame(surface = c(450, 320, 280), 
           price = c(12, 14, 6)), 
      flats = data.frame(surface = c(45, 89, 63), 
           price = c(4, 6, 9))) 

私は

for (type in c('houses', 'flats')){ 
    p <- ggplot(aes(x = surface, y = price), data = data[[type]]) +  
    geom_point() + 
    xlab(expression(paste('surface of this type /', m^{2}))) 
} 
p 

、表現を使用して「M^2」を表示するには、達成が、私はラベルに変数を追加しようとすると、次のことが、当然のことながら、動作しません。

for (type in c('houses', 'flats')){ 
    p <- ggplot(aes(x = surface, y = price), data = data[[type]]) +  
    geom_point() + 
    xlab(expression(paste('surface of ', type, '/', m^{2}))) 
} 
p 

提案がありますか?

答えて

9

それはbquoteで動作します:

xlab(bquote('surface of' ~ .(type) ~ '/' ~ m^{2})) 

enter image description here

関連する問題