R grid.arrange: nrow * ncol >= n 不为真
R grid.arrange: nrow * ncol >= n is not TRUE
我对函数 grid.arrange
有疑问。我想绘制一个数据集的箱线图,并在下面绘制一个数据表,其中包含最小值、平均值、中值和最大值。我收到以下错误消息:nrow * ncol >= n 不是 TRUE。这是我的代码:
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))
关于如何解决这个问题有什么想法吗?谢谢你帮助我!
我可以重现错误如下:
library(gridExtra)
library(grid)
grid.arrange(rectGrob(), rectGrob(), rectGrob(), ncol = 1, heights = c(1, 2))
#Error: nrow * ncol >= n is not TRUE
通过为每个地块指定高度很容易修复:
grid.arrange(rectGrob(), rectGrob(), rectGrob(), ncol = 1, heights = c(1, 2, 3))
或者您可以设置 ncol = 2
.
我对函数 grid.arrange
有疑问。我想绘制一个数据集的箱线图,并在下面绘制一个数据表,其中包含最小值、平均值、中值和最大值。我收到以下错误消息:nrow * ncol >= n 不是 TRUE。这是我的代码:
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))
关于如何解决这个问题有什么想法吗?谢谢你帮助我!
我可以重现错误如下:
library(gridExtra)
library(grid)
grid.arrange(rectGrob(), rectGrob(), rectGrob(), ncol = 1, heights = c(1, 2))
#Error: nrow * ncol >= n is not TRUE
通过为每个地块指定高度很容易修复:
grid.arrange(rectGrob(), rectGrob(), rectGrob(), ncol = 1, heights = c(1, 2, 3))
或者您可以设置 ncol = 2
.