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

编程入门 行业动态 更新时间:2024-10-28 05:23:53
本文介绍了在r中的ggplot的绘图区域内添加表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在ggplot中添加一个突出显示网站的坐标表。 使用以前的 question 作为示例数据:

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

我想将下表添加到图的右下角绘图区域。任何建议? $ p $ 表<-cbind(sites = c(site 1,site 2,site 3 ,site 4),mydata [10:13,])表 网站ab 网站1 10 -0.3053884 网站2 11 1.5117812 site 3 12 0.3898432 site 4 13 -0.6212406

解决方案

您可以使用 ggplot2 的 annotation_custom 与 tableGrob 来自 gridExtra 包。

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(color =blue)+ geom_point(data = mydata [10:13,],aes(x = a,y = b)红色,size = 5)+ annotation_custom(tableGrob(mytable),xmin = 35,xmax = 50,ymin = -2.5,ymax = -1) pre>

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

解决方案

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的绘图区域内添加表格

本文发布于:2023-07-18 05:06:46,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区域内   表格   ggplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!