gpt4 book ai didi

R grid.arrange : nrow * ncol >= n is not TRUE

转载 作者:行者123 更新时间:2023-12-01 00:46:52 25 4
gpt4 key购买 nike

我的功能有问题 grid.arrange .我想绘制一个数据集的箱线图,并在下面绘制一个带有最小值、平均值、中值和最大值的数据表。我收到以下错误消息:nrow * ncol >= n is not 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 .

关于R grid.arrange : nrow * ncol >= n is not TRUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33694159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com