2

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? 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))
Michael
  • 1,537
  • 6
  • 20
  • 42
  • 2
    Please provide a small, reproducable data example. It will be much easier helping you get the result you want. – SimonG Aug 17 '14 at 20:16

1 Answers1

3

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)

EDIT:

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

Quick PNG

SimonG
  • 4,701
  • 3
  • 20
  • 31
  • interesting. So in that case the numbers for the x-min and x-max values are discrete and for the y-min and y-man are continuous. However, changing those values doesn't move the table on the plot I am working with. :( – Michael Aug 17 '14 at 21:38
  • Updated my answer to include a solution to place the table (inside/outside the plot, doesn't matter) regardless of the type of plot. Let me know if that goes into the right direction. – SimonG Aug 17 '14 at 22:05
  • 1
    For the second option it might be easier to use `grid.arrange` For example, `grid.arrange(plt, my.tGrob, ncol=2)`. You can use the `widths` argument for a bit more control over spacing – user20650 Aug 17 '14 at 22:13
  • This would be less complicated for my example indeed. I chose the regular `grid` package because it would also allow placement inside the plot if that is desired. `grid.arrange` is limited here to side-by-side placements. – SimonG Aug 17 '14 at 22:16