由注解_自定义用geom_bar图创建的移动表 [英] Moving table created by annotation_custom with geom_bar plot

查看:106
本文介绍了由注解_自定义用geom_bar图创建的移动表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试搜索答案,但找不到任何东西.

I tried searching for answers but couldn't find anything.

我有一个图,想要在图本身内添加一个表格.我可以做到,但是桌子最终就在中间.

I have have a plot and want to add a table within the plot itself. I can do it but the table ends up being right in the middle.

如果x轴是离散的,是否可以重新放置由注解_自定义创建的表?如果可以,怎么办?

It is possible to relocate a table created by annotation_custom if the x-axis is discrete? If so, how?

谢谢!

例如,我想重新定位此表.

For example, I want to relocate this table.

library(ggplot2)
library(gridExtra)

my.summary <- summary(chickwts$weight)
my.table   <- data.frame(ids = names(my.summary), nums = as.numeric(my.summary))
ggplot(chickwts, aes(feed, weight)) +
       geom_bar(stat = "identity")  +
       annotation_custom(tableGrob(my.table))

推荐答案

ggplot2 中的自定义注释可以在绘图区域内重新整理.这至少将它们移出了中心.也许此解决方案已经对您足够了.我会尝试进行调整.也应该将其放置在绘图区域之外.

The custom annotation in ggplot2 can be rearragned inside the plotting area. This at least moves them out of the center. Maybe this solution is already sufficient for you. I'll try and tweak this. It should be possible to put this outside the plotting area as well.

library(ggplot2)
library(gridExtra)

my.summary <- summary(chickwts$weight)
my.table   <- data.frame(ids = names(my.summary), nums = as.numeric(my.summary))
ggplot(chickwts, aes(feed, weight)) +
       geom_bar(stat = "identity")  +
       annotation_custom(tableGrob(my.table), xmin=5,xmax=6,ymin=300,ymax=1300)

要将表格放置在绘图之外,无论绘图由什么组成,都可以使用 grid 包:

To place the table outside the plot, regardless of what the plot consists of, the grid package could be used:

library(ggplot2)
library(gridExtra)
library(grid)

# data
my.summary <- summary(chickwts$weight)
my.table   <- data.frame(ids = names(my.summary), nums = as.numeric(my.summary))

# plot items
my.tGrob <- tableGrob(my.table)
plt <- ggplot(chickwts, aes(feed, weight)) +
          geom_bar(stat = "identity")

# layout
vp.layout <- grid.layout(nrow=1, ncol=2, heights=unit(1, "null"),
  widths=unit(c(1,9), c("null","line")) )

# start drawing
grid.newpage()
pushViewport(viewport(layout=vp.layout, name="layout"))
# plot
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1, name="plot"))
print(plt, newpage=FALSE)
upViewport()
# table
pushViewport(viewport(layout.pos.row=1, layout.pos.col=2, name="table"))
grid.draw(my.tGrob)
upViewport()

#dev.off()

这篇关于由注解_自定义用geom_bar图创建的移动表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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