如何在ggplot2的轴标签中同时使用上标和变量

fst*_*ens 5 expression r superscript ggplot2

我想在n轴标签内一起使用一个变量(这里是矢量元素"type")和一个包含上标(这里是m ^ 2)的单元.

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))) 
Run Code Online (Sandbox Code Playgroud)

我用表达式实现显示"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
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在标签中添加变量时,以下内容当然不起作用:

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
Run Code Online (Sandbox Code Playgroud)

你有什么建议吗?

Sve*_*ein 11

它适用于bquote:

xlab(bquote('surface of' ~ .(type) ~ '/' ~ m^{2}))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述