gpt4 book ai didi

R:由于 UseMethod ("getModelInfo"中的错误,多图无法正常工作,模型)

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

在 R 中,我准备了多个 ggplot2 图形,它们被保存到这样的变量中:

flashplot <- ggplot(flash, aes(x=flash$Year, y=flash$Proc)) 
+ stat_smooth(method="lm", formula = "flash$Proc ~ poly(flash$Year, 2, raw=TRUE)", n=14, level=0.9)
+ geom_point(shape=19) + ylab("Percentage of total existing versions")
+ xlab("Years of Existence") + scale_x_continuous(breaks = c(0,2,4,6,8,10,12,14,16,18,20))

按预期绘制单个图形,但我想使用 multiplot 绘制 4 个图形(2 x 2),如 in the R cookbook 所述.

当我遇到错误时,我尝试了食谱中的示例,我认为它可以工作,但它给了我同样的错误:

> multiplot(p1, p2, p3, p4, cols=2)
Error in UseMethod("getModelInfo", model) :
no applicable method for 'getModelInfo' applied to an object of class "c('gg', 'ggplot')"

我在Windows 7下使用R 2.15.2,以及ggplot2coefplot的最新包:

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252
[3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C
[5] LC_TIME=German_Austria.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] coefplot_1.1.8 ggplot2_0.9.3

loaded via a namespace (and not attached):
[1] colorspace_1.2-0 dichromat_1.2-4 digest_0.6.0 grid_2.15.2
[5] gtable_0.1.2 labeling_0.1 MASS_7.3-22 munsell_0.4
[9] plyr_1.8 proto_0.3-9.2 RColorBrewer_1.0-5 reshape2_1.2.2
[13] scales_0.2.3 stringr_0.6.2 useful_1.1.6

任何帮助我解决我的问题的提示将不胜感激。我错过了什么吗?我已经在网上搜索过,但没有发现任何有用的信息

--根据要求,这里是示例数据:

library(ggplot2)

# This example uses the ChickWeight dataset, which comes with ggplot2
# First plot
p1 <-
ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
geom_line() +
ggtitle("Growth curve for individual chicks")

# Second plot
p2 <-
ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
geom_point(alpha=.3) +
geom_smooth(alpha=.2, size=1) +
ggtitle("Fitted growth curve per diet")

# Third plot
p3 <-
ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
geom_density() +
ggtitle("Final weight, by diet")

# Fourth plot
p4 <-
ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
geom_histogram(colour="black", binwidth=50) +
facet_grid(Diet ~ .) +
ggtitle("Final weight, by diet") +
theme(legend.position="none") # No legend (redundant in this graph)

multiplot(p1, p2, p3, p4, cols=2)

最佳答案

通过使用名为 grid.arrange() 的内置函数,有一个更简单的选项可以做到这一点。您需要安装名为 gridExtra 的软件包才能使用它:

grid.arrange(p1,p2,p3,p4, ncol = 2, main = "Main title")

Reference

关于R:由于 UseMethod ("getModelInfo"中的错误,多图无法正常工作,模型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788563/

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