0

I like to place a table and a plot in the same image. I am doing this:

my data frame x is this:

1/1/2010 10
1/2/2010 20
1/3/2010 15
1/4/2010 56
1/5/2010 46
1/6/2010 15
1/8/2010 15
1/9/2010 15
1/10/2010 20
1/11/2010 15
1/12/2010 15
1/13/2010 40
1/14/2010 15
1/15/2010 15
1/16/2010 70

p1<-plot(x)
p2<-tableGrob(x)

png("image1.png")
grid.arrange(p2, p1, main="Total Data and Image"
dev.off()

It works, but there seems to be a lot of space between p2 and p1. How can I have no or just little bit of space? Also, is there a way to make the font bigger on main?

thanks,

Sean Kaplan
  • 61
  • 2
  • 9
  • your code is not reproducible, what plotting package are you using? – baptiste May 22 '12 at 19:37
  • data is not important, it could be anything. I am trying to show data in table and plot format in the same image. Space between grids are huge. I am using library(gridExtra) – Sean Kaplan May 22 '12 at 19:51
  • @SeanKaplan: It is helpful to have a reproducible example so others can understand what you mean by "a lot of space between p2 and p1". It will help focus the discussion. – jthetzel May 22 '12 at 20:14
  • I think I found a work around. There may be other solutions but I thinks this works for me. thanks again. grid.arrange(arrangeGrob(p2, p1, main="Total Data and Image", clip=TRUE, widths=c(1,2), heights=c(1,2))) – Sean Kaplan May 22 '12 at 20:28
  • this last line is rather absurd; anyway, knowing the plotting package is important if you want an answer. – baptiste May 23 '12 at 01:40

2 Answers2

3

This is not an answer.

Your code is not reproducible. Wrapping your data in a read.table() function (and accepting default variable names), guessing that you are using base graphics, fixing typos in your code, and loading the gridExtra package (necessary for the grid.arrange function), I still cannot get your code to work. Using the ggplot2 package to draw the graph, I can get your code to work. The code is now reproducibe. See here for how to make a great reproducible example. And using my code, the result in my opinion looks pretty good. But it might not be what you intended. That's why the community asks you to generate a reproducible example. All the better then to see what you mean by "a lot of space between p2 and p1".

But taking your workaround from your comment above - there are unnecessary elements in the code. You do not need both grid.arrange and arrangeGrob. Also, because you have the elements arranged side-by-side, I don't think you need both widths and heights; widths alone is sufficient.

library(ggplot2)
library(gridExtra)

x = read.table(text = "
1/1/2010 10
1/2/2010 20
1/3/2010 15
1/4/2010 56
1/5/2010 46
1/6/2010 15
1/8/2010 15
1/9/2010 15
1/10/2010 20
1/11/2010 15
1/12/2010 15
1/13/2010 40
1/14/2010 15
1/15/2010 15
1/16/2010 70", sep = "", header = FALSE)

p1<-ggplot(x, aes(V2, V1)) + geom_point()
p2<-tableGrob(x)
grid.arrange(p2, p1, main="Total Data and Image", ncol = 2)

Your fix:

grid.arrange(p2, p1, main="Total Data and Image", ncol = 2, widths=c(1,2))

Edit Baptiste's solution - see comments below:

  grid.arrange(p2, p1, main=textGrob("Total Data and Image", gp=gpar(cex=3)), ncol = 2,
   widths=unit.c(grobWidth(p2), unit(1,"npc") - grobWidth(p2)))
Community
  • 1
  • 1
Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
  • 1
    +1 Easier yet, `grid.arrange(left=p2, p1, main=textGrob("Total Data and Image", gp=gpar(cex=3)))` since `tableGrob` has an absolute width. The title wouldn't be centered anymore though, but that gives the idea to define the widths as `unit.c(grobWidth(p2), unit(1,"npc") - grobWidth(p2))` – baptiste May 23 '12 at 08:06
0

To increase the font size on main use:

plot(x, y , cex.main=3)

Have a look at this post and you'll idea about the font sizes: Plotting in R software, how to magnify axis values in a large PNG file

Community
  • 1
  • 1
Swapnil 'Tux' Takle
  • 1,187
  • 2
  • 9
  • 9