【问题标题】:How to modify axis labels within ggplot labs()如何在 ggplot labs() 中修改轴标签
【发布时间】:2021-12-09 20:08:32
【问题描述】:

假设我想用 str_to_title() 函数修改一个 ggplot 轴标签。

library(tidyverse)

mtcars %>% 
    ggplot(aes(x = wt, y = mpg)) + 
    geom_point() + 
    labs(x = ~str_to_title(.x))

我的 x 轴不会被标记为“Wt”,而是会被标记为“str_to_title(.x)”。有没有办法在 labs() 函数中应用函数?

【问题讨论】:

    标签: r ggplot2 stringr


    【解决方案1】:

    labs 不像ggplot2 的许多其他组件那样执行程序化 NSE。一种选择是以编程方式定义列,使用aes_as.name(或other ways too)就可以了。

    library(ggplot2)
    library(stringr) # str_to_title
    xx <- "wt"; yy <- "mpg"
    ggplot(mtcars, aes_(x = as.name(xx), y = as.name(yy))) + 
      geom_point() + 
      labs(x = str_to_title(xx))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2021-10-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2014-08-16
      • 2015-07-12
      相关资源
      最近更新 更多