質問

I'm trying to arrange plots and tables together, but the tables get cut off both on top and on the sides.

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

The text for the plot resize accordingly, but how can the same be done for the tables?

役に立ちましたか?

解決

This solution is pretty ugly, but it does work. I just messed with the numbers in the heights and widths arguments until I got something that fit into the window.

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

If I were to spend more time on this, I would look into ?viewport

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top