首页 文章

在r中的ggplot的绘图区域内添加表格

提问于
浏览
33

我想在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)

enter image description here

我想将下表添加到绘图区域内绘图的右下角 . 任何建议?

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

4 回答

  • 5

    您可以将 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)
    

    enter image description here

  • 1

    @ user3206440,@ Bulletin存在删除行号的简单方法:将 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)
    

    Plot image

    请参阅gridExtra Wiki .

  • 57

    @ 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)
    
  • 1

    随着'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完全支持tibbles(参见release announcement),可以将数据帧列表映射到 label 美学 . 包'ggpmisc 0.3.0'中的新 geom_table() 利用了这一点,使得可以添加具有类似于 geom_label() 语法的表(参见documentation) . 这里似乎最适合将表添加为注释,但当然 geom_table() 也可以直接用作其他 ggplot 几何 .

    enter image description here

相关问题