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