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

标签 r ggplot2 expression superscript

我想一起使用一个变量(这里是向量元素“类型”)和一个包含 n 轴标签内的上标(这里是 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))) 

我实现了使用表达式显示“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

你有什么建议吗?

最佳答案

它适用于 bquote :

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

enter image description here

关于r - 如何使用ggplot2在轴标签中同时使用上标和变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20005233/

相关文章:

r - 使用客户端处理(服务器 = F)在 Shiny 应用程序中进行 DT 编辑会抛出 JSON 错误

r - 在 R 中进行子集化的最快方法

R + ggplot + pdf 设备 + LaTeX : is it possible to embed fonts one time

r - 组的每个成员都连接到 r 中的中心和所有集群大中心的结构图

c# - 是否有用于 C# 的 DataBinder.Eval 的快速版本?

r - 计算几行并在 R 中创建一个新列

r - 如何在 ggplotly 中为条形图更改悬停背景颜色

R ggplot2 : How can I name the y axis depending on the value of a variable with facet_wrap?

mysql - QlikView 表达式查询

c# - 返回委托(delegate)的 Lambda 表达式