【问题标题】:How to use simultaneously superscript and variable in a axis label with ggplot2如何在带有ggplot2的轴标签中同时使用上标和变量
【发布时间】:2013-11-15 16:00:25
【问题描述】:

我想在 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

你有什么建议吗?

【问题讨论】:

    标签: r ggplot2 expression superscript


    【解决方案1】:

    它适用于bquote:

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

    【讨论】:

      猜你喜欢
      • 2015-02-11
      • 2021-07-23
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多