在 r 中 ggplot 的绘图区域内添加表格 [英] Adding table within the plotting region of a ggplot in r

查看:31
本文介绍了在 r 中 ggplot 的绘图区域内添加表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to add a table of the coordinates of highlighted site in a ggplot.

使用之前的问题作为示例数据:

Using a previous question as example data:

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)

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

I would like to add the following table to the lower right-hand corner of the plot within the plotting region. Any advice?

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

推荐答案

您可以将 ggplot2annotation_customtableGrob 一起使用gridExtra 包.

You can use ggplot2's annotation_custom with a tableGrob from the gridExtra package.

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)

这篇关于在 r 中 ggplot 的绘图区域内添加表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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