首页 > 解决方案 > R如何将列名添加为标题

问题描述

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 轴标签还是绘图标题(名称(。))不起作用。

标签: rggplot2dplyr

解决方案


这可以通过使用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)


推荐阅读