【问题标题】:visual structure of a data.frame: locations of NAs and much moredata.frame 的视觉结构:NA 的位置等等
【发布时间】:2015-02-17 04:07:45
【问题描述】:

我想用颜色编码在单个图上表示数据框(或矩阵,或 data.table 等)的结构。我想这对于许多处理各种类型数据的人来说可能非常有用,一目了然地可视化它。

也许有人已经开发了一个包来做这件事,但我找不到(只是this)。所以这是我的“愿景”的粗略模型,有点像热图,以颜色代码显示:

  • 北美地区,
  • 变量类别(因子(多少级?)、数值(带有颜色渐变、零、异常值...)、字符串)
  • 尺寸
  • 等等.....

到目前为止,我刚刚编写了一个函数来绘制 NA 位置,如下所示:

ggSTR = function(data, alpha=0.5){
  require(ggplot2)
  DF <- data
  if (!is.matrix(data)) DF <- as.matrix(DF)

  to.plot <- cbind.data.frame('y'=rep(1:nrow(DF), each=ncol(DF)), 
                              'x'=as.logical(t(is.na(DF)))*rep(1:ncol(DF), nrow(DF)))
  size <- 20 / log( prod(dim(DF)) )  # size of point depend on size of table
  g <- ggplot(data=to.plot) + aes(x,y) +
        geom_point(size=size, color="red", alpha=alpha) +
        scale_y_reverse() + xlim(1,ncol(DF)) +
        ggtitle("location of NAs in the data frame")

  pc <- round(sum(is.na(DF))/prod(dim(DF))*100, 2) # % NA
  print(paste("percentage of NA data: ", pc))

  return(g)
}

它接受输入中的任何 data.frame 并返回此图像:

第一个图像对我来说挑战太大了。

【问题讨论】:

标签: r dataframe ggplot2 na missing-data


【解决方案1】:

你遇到过CSV fingerprint service吗?它创建了一个类似的图像,尽管没有包含您上面概述的所有细节,并且它不是基于 R。R-ohjelmointi.org 有类似想法的 R 版本,但文本是芬兰语。主要功能是csvSormenjalki()。也许可以进一步调整以实现您的整个愿景?

【讨论】:

  • 确实,这是一个我不知道的好工具,谢谢。正如你所说,我想挑战是让它适应 R
【解决方案2】:

我知道有一个包很容易显示缺失值,但我的 google-fu 目前不是很好。但是,我确实找到了一个名为 tableplot 的函数,它可以让您全面了解您的数据框。我不知道它是否会显示丢失的数据。

这是链接:

http://www.ancienteco.com/2012/05/quickly-visualize-your-whole-dataset.html

【讨论】:

  • 刚刚测试过,这里做得很好;但它在探索性分析过程中可能更“下游”,因为它已经对数据进行排序并更改结构以显示直方图。 +1
  • library(DescTools) 现在有一个函数:PlotMiss
【解决方案3】:

最终我想出了一个脚本来绘制大部分规范。我在这里提交它,尽管语法远非“优雅”,但有些人可能会感兴趣!

请注意,主函数 'colstr' 有 3 个参数: - 输入(df 或矩阵甚至单个向量) - 要绘制的最大行数 - 导出为 png 到工作目录的选项。

输出给出,例如:

# PACKAGES 
require(ggplot2)
require(RColorBrewer)
require(reshape2)

# Test if an object is empty (data.frame, matrix, vector)
is.empty = function (input) {
  df <- data.frame(input)
  (is.null(df) || nrow(df) == 0 || ncol(df) == 0 || NROW(df) == 0)
}

#  min/max normalization (R->[0;1]), (all columns must be numerical)
minmax <- function(data, ...) {
  .minmax = function(x) (x-min(x, ...))/(max(x, ...)-min(x, ...))
  # find constant columns, replaces with O.5:
  constant <- which(apply(data, 2, function(u) {min(u, ...)==max(u, ...)}))
  if(is.vector(data)) {
    res <- .minmax(data)
  } else {
    res <- apply(data, 2, .minmax)
  }
  res[, constant] <- 0.5
  return(res)
}

# MAIN function
colstr = function(input, size.max=500, export=FALSE) {
  data      <- as.data.frame(input)
  if (NCOL(data) == 1) {
    data    <- cbind(data, data)
    message("warning: input data is a vector")
  }
  miror     <- data # miror data.frame will contain a coulour coding for all cells
  wholeNA   <- which(sapply(miror, function(x) all(is.na(x))))
  whole0    <- which(sapply(miror, function(x) all(x==0)))
  numeric   <- which(sapply(data, is.numeric))
  character <- which(sapply(data, is.character))
  factor    <- which(sapply(data, is.factor))
  # characters to code
  miror[character] <- 12 
  # factor coding
  miror[factor] <- 11
  # min/max normalization, coerce it into 9 classes.
  if (!is.empty(numeric)) {miror[numeric] <- minmax(miror[numeric], na.rm=T)}
  miror[numeric] <- data.frame(lapply(miror[numeric], function(x) cut(x, breaks=9, labels=1:9))) # 9 classes numériques
  miror <- data.frame(lapply(miror, as.numeric))
  # Na coding
  miror[is.na(data)] <- 10
  miror[whole0]    <- 13
  # color palette vector
  mypalette <- c(brewer.pal(n=9, name="Blues"), "red", "green", "purple", "grey")
  colnames <- c(paste0((1:9)*10, "%"), "NA", "factor (lvls)", "character", "zero")
  # subset if too large
  couper <- nrow(miror) > size.max
  if (couper) miror <- head(miror, size.max)
  # plot
  g <- ggplot(data=melt(as.matrix(unname(miror)))) + 
    geom_tile(aes(x=Var2, y=Var1, fill=factor(value, levels=1:13))) +
    scale_fill_manual("legend", values=mypalette, labels=colnames, drop=FALSE) +
    ggtitle(paste("graphical structure of", deparse(substitute(input)), paste(dim(input), collapse="X"), ifelse(couper, "(truncated)", ""))) +
    xlab("columns of the dataframe") + ylab("rows of the dataframe") +
    geom_point(data=data.frame(x=0, y=1:NROW(input)), aes(x,y), alpha=1-all(row.names(input)==seq(1, NROW(input)))) +
    scale_y_reverse(limits=c(min(size.max, nrow(miror)), 0))
  if (!is.empty(factor)) {
    g <- g + geom_text(data=data.frame(x     = factor, 
                                       y     = round(runif(length(factor), 2, NROW(miror)-2)), 
                                       label = paste0("(", sapply(data[factor], function(x) length(levels(x))), ")")),
                       aes(x=x, y=y, label=label))
  }
  if (export) {png("colstr_output.png"); print(g); dev.off()}
  return(g)
}

【讨论】:

    【解决方案4】:

    你可以试试visdat包(https://github.com/ropensci/visdat),它显示了图中的NA值和数据类型

    install.packages("visdat")
    library(visdat)
    vis_dat(airquality)
    

    【讨论】:

    • 感谢您的回答,漂亮的包和输出很整洁,是最近的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多