【问题标题】:R how to add column name as titleR如何将列名添加为标题
【发布时间】:2020-11-24 19:33:34
【问题描述】:
plots <- data %>% 
  select( contains("Pct")) %>%
  map(~ggplot(data, aes(y= . , x = Project_Name ,fill=Project_Name,)) +
    geom_boxplot(alpha=0.7) +
    theme(legend.position="none", axis.title.x=element_blank() , axis.text.x=element_blank() )+
    labs(title=names(.)) +
    scale_fill_brewer(palette="Dark2"))

ggarrange(plotlist = plots,common.legend = TRUE)

使用上面的代码,我可以得到绘图但不能得到 y 轴标签,我可以将选定的列名称添加为 y 轴标签还是绘图标题 (names(.)) 没有没用。

【问题讨论】:

    标签: r ggplot2 dplyr


    【解决方案1】:

    这可以通过使用purrr::imap 而不是map 来实现。

    使用iris 作为示例数据试试这个:

    library(ggplot2)
    library(dplyr)
    library(ggpubr)
    library(purrr)
    
    plots <- iris %>% 
      select(contains("Petal")) %>%
      imap(~ggplot(iris, aes(y = .x , x = Species , fill=Species)) +
            geom_boxplot(alpha=0.7) +
            theme(legend.position="none", axis.title.x=element_blank() , axis.text.x=element_blank() )+
            labs(title=.y) +
            scale_fill_brewer(palette="Dark2"))
    
    ggarrange(plotlist = plots, common.legend = TRUE)
    

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2022-07-05
      • 2016-12-07
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多