从列表中数据框的名称中将标题名称添加到ggplots列表中 [英] Add title name to list of ggplots from the names of dataframes in list

查看:42
本文介绍了从列表中数据框的名称中将标题名称添加到ggplots列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据框列表中生成箱形图列表:

I am producing a list of boxplots from a list of data frames as so:

Bps<-lapply(dflist, function(x){
  gg<-ggplot(x, aes(x= x, y= y, fill= x))+
   geom_boxplot(position  = "dodge")+
    ggtitle(names(x))    
 })

数据帧列表可以通过以下方式生成:

The list of dataframes can be generated by:

 df1<-data.frame(x=seq(1:50), y=rep("Blank", "Non_blank",25))
 df2<-data.frame(x=seq(1:40), y=rep("Blank", "Non_blank",20))
 df3<-data.frame(x=seq(1:30), y=rep("Blank", "Non_blank",15))

dflist<-list(df1,df2,df3

我需要粘贴原始数据框的名称作为每个箱形图的标题.因此,如果dflist具有3个称为dfA,dfB和dfC的数据帧,则这些数据帧应该是由lapply函数制作的每个箱形图的标题.我似乎只能得到一列的名称,而不是原始的数据框名称.非常感谢,M

I need to paste the names of the original dataframes as the title for each boxplot. So if dflist has 3 dataframes called dfA, dfB and dfC, these should be the titles of each boxplot made from the lapply function. I can only seem to get the name of a column and not the original dataframe name. Many thanks, M

推荐答案

由于这是一个顺序问题,我们不需要分配结果,因此可以使用 for循环:

Since this is a sequential problem and we don't need to assign the outcome, we can use a for loop:

names(dflist) <- c("dfA", "dfB", "dfC") # Define names of df's

for (i in seq(dflist)) {
  gg <- ggplot(dflist[[i]], aes(x = x, y = y, fill = x))+
    geom_boxplot(position  = "dodge") +
    ggtitle(names(dflist[i]))
  print(gg)
}

这篇关于从列表中数据框的名称中将标题名称添加到ggplots列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆