I have a problem with the function grid.arrange
. I would like to plot a boxplot of a dataset and plot a datatable below with the minimum, mean, median and maximum values. I'm getting the following error message: nrow * ncol >= n is not TRUE. Here is my code:
df.plot <- ggplot((Boxplot_Flaechenpotential_2_2), aes(x=GMDKLASSE,y=Flaeche)) +
ylab("Flächenpotential (1000m2)") + xlab(" ") +
geom_boxplot() + theme_bw() +
ggtitle(" ") +
theme(plot.title = element_text(lineheight=.8, face="bold"),
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
lab.df<-data.frame(lab1=c("Kleine Gemeinde","Mittlere Gemeinde","Grosse Gemeinde"),
lab2=c("1","2","38"),
lab3=c("55","78","176"),
lab4=c("67","96","233"),
lab5=c("214","443","1042"))
lab.df$lab1 <- factor(lab.df$lab1, levels=lab.df$lab1)
lab.df$lab2 <- factor(lab.df$lab2, levels=lab.df$lab2)
lab.df$lab3 <- factor(lab.df$lab3, levels=lab.df$lab3)
lab.df$lab4 <- factor(lab.df$lab4, levels=lab.df$lab4)
lab.df$lab5 <- factor(lab.df$lab5, levels=lab.df$lab5)
df.table1 <- ggplot(lab.df, aes(x = lab1, y = 0,
label = lab1)) +
geom_text(size = 4.5, colour = "black") +
theme_minimal() +
scale_y_continuous(breaks=NULL, name = " ") +
theme(panel.grid.major = element_blank(), legend.position = "none",
panel.border = element_blank(), axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_text(angle = 0, hjust = 1.2))
df.table2 <- ggplot(lab.df, aes(x = lab2, y = 0,
label = lab2)) +
geom_text(size = 4.5, colour = "black") +
theme_minimal() +
scale_y_continuous(breaks=NULL, name = "Tiefster Wert")+
theme(panel.grid.major = element_blank(), legend.position = "none",
panel.border = element_blank(), axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_text(angle = 0, hjust = 3.0))
df.table3 <- ggplot(lab.df, aes(x = lab3, y = 0,
label = lab3)) +
geom_text(size = 4.5, colour = "black") +
theme_minimal() +
scale_y_continuous(breaks=NULL, name = "Mittelwert")+
theme(panel.grid.major = element_blank(), legend.position = "none",
panel.border = element_blank(), axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_text(angle = 0, hjust = 3.0))
df.table4 <- ggplot(lab.df, aes(x = lab4, y = 0,
label = lab4)) +
geom_text(size = 4.5, colour = "black") +
theme_minimal() +
scale_y_continuous(breaks=NULL, name = "Median")+
theme(panel.grid.major = element_blank(), legend.position = "none",
panel.border = element_blank(), axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_text(angle = 0, hjust = 3.0))
df.table5 <- ggplot(lab.df, aes(x = lab5, y = 0,
label = lab5)) +
geom_text(size = 4.5, colour = "black") +
theme_minimal() +
scale_y_continuous(breaks=NULL, name = "Höchster Wert")+
theme(panel.grid.major = element_blank(), legend.position = "none",
panel.border = element_blank(), axis.text.x = element_blank(),
axis.ticks = element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_text(angle = 0, hjust = 3.0))
gA <- ggplotGrob(df.plot)
gB <- ggplotGrob(df.table1)
gC <- ggplotGrob(df.table2)
gD <- ggplotGrob(df.table3)
gE <- ggplotGrob(df.table4)
gF <- ggplotGrob(df.table5)
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3], gC$widths[2:3], gD$widths[2:3], gE$widths[2:3], gF$widths[2:3])
gA$widths[2:3] <- as.list(maxWidth)
gB$widths[2:3] <- as.list(maxWidth)
gC$widths[2:3] <- as.list(maxWidth)
gD$widths[2:3] <- as.list(maxWidth)
gE$widths[2:3] <- as.list(maxWidth)
gF$widths[2:3] <- as.list(maxWidth)
grid.arrange(gA, gB, gC, gD, gE, gF, ncol=1, heights=c(5, 0.6, 0.6))
Any ideas on how to solve this? Thanks for helping me!
I can reproduce the error as follows:
library(gridExtra)
library(grid)
grid.arrange(rectGrob(), rectGrob(), rectGrob(), ncol = 1, heights = c(1, 2))
#Error: nrow * ncol >= n is not TRUE
It's easily fixed by specifying a height for each plot:
grid.arrange(rectGrob(), rectGrob(), rectGrob(), ncol = 1, heights = c(1, 2, 3))
Or you could set ncol = 2
.