2013-11-15 13 views
5

Vorrei utilizzare insieme una variabile (qui l'elemento vettore "tipo") e un'unità contenente un apice (qui m^2) all'interno dell'etichetta dell'asse n.Come utilizzare contemporaneamente apice e variabile in un'etichetta dell'asse con 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))) 

raggiungo per visualizzare "m^2" utilizzando un'espressione,

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 

ma quando provo ad aggiungere la variabile nell'etichetta, quanto segue, naturalmente, non funziona:

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 

Avete un suggerimento?

risposta

9

Funziona con bquote:

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

enter image description here

Problemi correlati