如何将summary()的结果显示为绘图/绘制? [英] How to display results of summary() as plot/grob?

查看:54
本文介绍了如何将summary()的结果显示为绘图/绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我有一个包含50多个要素的数据集,出于演示目的,我想为每个要素生成一个箱线图,直方图和摘要统计信息.制作了150多个地块.我用于执行上述操作的代码如下:

Context: I have a dataset of 50+ features, and I would like to produce a boxplot, histogram, and summary statistic for each of them, for presentation purposes. That makes 150+ plots. The code I have used to do the above mentioned is as such:

library(ggplot2)
library(dplyr)
library(ggpubr)
library(ggthemes)
library(Rmisc)
library(gridExtra)    

myplots <- list()  # new empty list

for (i in seq(2,5,3)){
  local({
    i <- i
    p1 <- ggplot(data=dataset,aes(x=dataset[ ,i], colour=label))+ 
      geom_histogram(alpha=.01, position="identity",bins = 33, fill = "white") +
      xlab(colnames(dataset)[ i]) + scale_y_log10()  + theme_few()
    p2<- ggplot(data=dataset, aes( x=label, y=dataset[ ,i], colour=label)) +
      geom_boxplot()+ylab(colnames(dataset)[ i]) +theme_few()
    p3<- summary(dataset[ ,i])
    print(i)
    print(p1)
    print(p2)
    print(p3)
    myplots[[i]] <<- p1  # histogram
    myplots[[i+1]] <<- p2 # boxplot
    myplots[[i+2]] <<- p3 # summary
  })
}

myplots[[2]]
length(myplots)

n <- length(myplots)
nCol <- floor(sqrt(n))
do.call("grid.arrange", c(myplots, ncol=nCol)) # PROBLEM: cant print summary as  grob

我创建了一个绘图列表,每3个元素代表每个特征的直方图,箱线图和摘要的结果.我遍历了50多个功能中的每一个,将每个结果附加到我的列表中(这不是我所知道的执行此操作的最佳方法).然后,当我尝试通过网格排列打印列表时遇到以下问题:

I have created a list of plots, every 3 elements represent the results of a histogram, boxplot, and summary for each feature. I iterate through each of the 50+ features, appending each of the results to my list (not the best way to go about doing this I know). I then run into the following issue when I attempt to print the list through grid arrange:

Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1,  : 
  only 'grobs' allowed in "gList"

这是可以理解的,因为摘要功能不会产生图形对象.除了完全不包括摘要统计信息之外,关于如何克服这一挫折的任何想法?

Understandably so, as the summary function does not produce a graphical object. Any ideas as to how I can overcome this setback apart from not including summary statistics at all?

推荐答案

在这里结合了一些建议之后,我设法弄清楚了如何遍历不同的对象,然后将每个功能的摘要统计信息绘制为grob对象.我的数据集的特征.

Hi after combining several of the suggestions here i managed to figure out how to go about plotting the summary statistics per feature as a grob object, after looping through the different features of my dataset.

library(skimr)
library(GridExtra)
library(ggplot2)
library(dplyr)
mysumplots <- list() # new empty list

for (i in seq(2,ncol(dataset))){
  local({
    i <-         
    sampletable <- data.frame(skim((dataset[ ,i]))) #creates a skim data frame 
    summarystats<-select(sampletable, stat, formatted) #select relevant df columns
    summarystats<-slice(summarystats , 4:10) #select relevant stats
    p3<-tableGrob(summarystats, rows=NULL) #converts df into a tableGrob

    mysumplots[[i]] <<- p3 # summary #appends the grob of to a list of summary table grobs
  })
}

do.call("grid.arrange", c(mysumplots, ncol=3)) # use grid arrange to plot all my grobs

这是为每个列(功能)创建一个略读数据框,然后我选择了相关统计信息,并将该grob分配给变量p3,然后将其迭代地附加到每个功能的表格列表中.然后,我使用gridarrange将所有tableGrobs打印出来!

What this does is create a skim dataframe of each column (feature), then i selected the relevant statistics, and assigned that grob to the variable p3, which is then iteratively appended to a list of tablegrobs for each feature. I then used gridarrange to print all of the tableGrobs out!

这篇关于如何将summary()的结果显示为绘图/绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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