2013-11-03 33 views
0

我試圖安排情節和表在一起,但表都得到了切斷頂部和兩側。情節和表在一起

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

# Generate example data 
plot.data <- cbind(c(1,2,3,4,5,6,7,8,9), c(2,2,2,3,3,3,4,4,4)) 
colnames(plot.data) <- c("the.x", "the.y") 
plot.data <- data.frame(plot.data) 
p <- ggplot(plot.data, aes(x=the.x, y=the.y)) 
p <- p + geom_line() 
table.1.data <- cbind("quitelongparametertext", seq(from=1, to=15), seq(from=21, to=35)) 
colnames(table.1.data) <- c("parameter", "data.1", "data.2") 
table.1.data <- data.frame(table.1.data) 
table.2.data <- cbind("shortertext", seq(from=1, to=3), seq(from=11, to=13), seq(from=21, to=23), seq(from=31, to=33), seq(from=41, to=43), seq(from=51, to=53)) 
colnames(table.2.data) <- c("parameter", "data.1", "data.2", "data.3", "data.4", "data.5", "data.6") 
table.2.data <- data.frame(table.2.data) 
t1 <- tableGrob(table.1.data, show.rownames=F) 
t2 <- tableGrob(table.2.data, show.rownames=F) 

# Print together 
print(arrangeGrob(p, arrangeGrob(t1, t2, nrow=2), ncol=2, widths=c(1,1))) 

劇情的文字相應地調整大小,但是表格也可以如何調整?

+1

或許這可以幫助? http://stackoverflow.com/questions/15826831/r-adding-a-datatable-to-a-ggplot-graph-using-viewports-scaling-the-grob/15828375#15828375 – JT85

+0

謝謝。我遵循這些例子(非常好),但我不確定如何將它應用於我所具有的特定問題。 – Chris

回答

1

該解決方案非常難看,但確實有效。我只是混淆了heightswidths的論點中的數字,直到我找到適合窗口的東西。

print(arrangeGrob(p, arrangeGrob(t1, t2, nrow=2, heights=c(1, 1/2.7)), widths=c(1/3.3,3/4.4),ncol=2))

如果我花這個更多的時間,我會去了解一下?viewport

+0

謝謝。我會考慮視口。感謝您指出方向。 – Chris