【问题标题】:Adding table within the plotting region of a ggplot in r在 r 中的 ggplot 的绘图区域内添加表格
【发布时间】:2012-09-01 08:07:00
【问题描述】:

我想在 ggplot 中添加一个突出显示站点的坐标表。

使用以前的question 作为示例数据:

set.seed(1)
mydata <- data.frame(a=1:50, b=rnorm(50))
ggplot(mydata,aes(x=a,y=b)) + 
    geom_point(colour="blue") + 
    geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5)

我想将下表添加到绘图区域内绘图的右下角。有什么建议吗?

table<-cbind(sites=c("site 1","site 2","site 3","site 4"),mydata[10:13,])
table

    sites  a          b
    site 1 10 -0.3053884
    site 2 11  1.5117812
    site 3 12  0.3898432
    site 4 13 -0.6212406

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    您可以将ggplot2annotation_customgridExtra 包中的tableGrob 一起使用。

    library(ggplot2)
    library(gridExtra)
    set.seed(1)
    mydata <- data.frame(a=1:50, b=rnorm(50))
    mytable <- cbind(sites=c("site 1","site 2","site 3","site 4"),mydata[10:13,])
    k <- ggplot(mydata,aes(x=a,y=b)) + 
      geom_point(colour="blue") + 
      geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5) + 
      annotation_custom(tableGrob(mytable), xmin=35, xmax=50, ymin=-2.5, ymax=-1)
    

    【讨论】:

    • 不错。不过,我会从b 中删除至少四位小数,除非您真的关心 10,000 精度中的一部分(提示:除非飞往火星,否则您不会这样做)。
    • @mplourde 谢谢!这是一个非常好的和直接的解决方案。为了让桌子更加突出,是否可以在其周围放置框架?
    • 看看?tableGrob 看看你可以控制的美学。使用show.box=TRUE 将框住单元格。
    • 对美学的控制在上一个版本中发生了变化,请参阅the vignette 了解各种新选项。
    • 是否可以在显示时删除行号?
    【解决方案2】:

    随着'ggplot2' 3.0.0 和'ggpmisc' 0.3.0 的发布,一个新的更简单的答案是可能的:

    library(ggplot2)
    library(ggpmisc)
    
    set.seed(1)
    mydata <- data.frame(a = 1:50, b = rnorm(50))
    table <- cbind(sites=c("site 1", "site 2", "site 3", "site 4"), mydata[10:13, ])
    
    ggplot(mydata, aes(x = a, y = b)) + 
      geom_point(colour = "blue") + 
      geom_point(data = mydata[10:13, ], aes(x = a, y = b), colour = "red", size = 5) +
      annotate(geom = "table", x = 37, y = -0.8, label = list(table), 
               vjust = 1, hjust = 0)
    

    'ggplot2' 3.0.0 完全支持小标题(参见release announcement),可以将数据框列表映射到label 美学。 'ggpmisc 0.3.0' 包中的新geom_table() 充分利用了这一点,使得添加语法类似于geom_label() 的表成为可能(参见documentation)。这里似乎最适合将表格添加为 注解,当然geom_table()也可以直接使用,和其他ggplot几何一样。

    【讨论】:

      【解决方案3】:

      @user3206440, @Punintended 存在删除行号的简单方法:将rows = NULL 添加到对tableGrob 的调用中。

      library(ggplot2)
      library(gridExtra)
      set.seed(1)
      mydata <- data.frame(a=1:50, b=rnorm(50))
      mytable <- 
         cbind(sites=c("site 1","site 2","site 3","site 4"), mydata[10:13,])
      k <- ggplot(mydata,aes(x=a,y=b)) + 
        geom_point(colour="blue") + 
        geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5) + 
        annotation_custom(tableGrob(mytable, rows=NULL), 
                          xmin=35, xmax=50, ymin=-2.5, ymax=-1)
      

      请查看gridExtra Wiki

      【讨论】:

        【解决方案4】:

        @user3206440:我找到了一种解决方法,可以删除行号。我将数据格式化为矩阵,分配列名,然后让 tableGrob 直接调用它。每当我让 tableGrob 将其作为数据框调用时,行名都会保留。

        下面是一个例子。我确信有一种更简单的方法来处理 chisq.test 输出

        chivalues <- chisq.test(chitable)
        chidf <- matrix(c(unlist(c(round(as.numeric(chivalues[1]), 2),
                            chivalues[2], round(as.numeric(chivalues[3]), 5), numcells))),
                           nrow = 1, ncol = 4, byrow = FALSE)
        colnames(chidf) <- c("Chi-Squared", "DF", "P Value", "Total Cells")
        

        后来……

        annotation_custom(tableGrob(chidf), xmin=2, xmax=6, ymin=700, ymax=800)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-12
          • 2012-08-20
          • 1970-01-01
          • 1970-01-01
          • 2012-04-29
          • 1970-01-01
          相关资源
          最近更新 更多