2013-11-15 24 views
5

के साथ अक्ष अक्ष में एक साथ सुपरस्क्रिप्ट और चर का उपयोग कैसे करें मैं एन अक्ष लेबल के अंदर एक चर (यहां वेक्टर तत्व "प्रकार") और एक सुपरस्क्रिप्ट (यहां एम^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))) 

मैं प्रदर्शित करने के लिए "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 this type /', m^{2}))) 
} 
p 

लेकिन जब मैं लेबल में चर जोड़ने की कोशिश, निम्नलिखित, ज़ाहिर है, काम नहीं करता है को प्राप्त:

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