简体   繁体   English

由注解_自定义用geom_bar图创建的移动表

[英]Moving table created by annotation_custom with geom_bar plot

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. 我可以做到,但是桌子最终就在中间。

It is possible to relocate a table created by annotation_custom if the x-axis is discrete? 如果x轴是离散的,是否可以重定位由ationment_custom创建的表? If so, how? 如果是这样,怎么办?

Thank you! 谢谢!

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))

The custom annotation in ggplot2 can be rearragned inside the plotting area. ggplot2的自定义注释可以在绘图区域内重新整理。 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)

EDIT: 编辑:

To place the table outside the plot, regardless of what the plot consists of, the grid package could be used: 要将表格放置在绘图之外,无论绘图由什么组成,都可以使用grid包:

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()

快速PNG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM