我正在尝试使用网格排列和12个ggplot对象创建一个3X4图
有来自3个数据帧的数据被用来制作这个更大的图,因此从每个数据集创建4个ggplot对象
这是我的代码:
dataframes <- list(Healthy, patdata, post_patdata) # making list of dataframes to use in for-loop
data_label<- c("Healthy","Pre-Therapy", "Post-Therapy" ) # another vector used for labeling plots
col<- c("blue","red","green") # 3rd vector used for picking plot color
PC1p <- vector("list",12)
for (j in 1:3){
dataset= dataframes[[j]]
# PC1
PC1p[[1+(j-1)*4]]=ggplot(dataset,aes(dataset$PC1w1)) +
geom_histogram(alpha=0.3,bins=20, fill=I(col[j]),col=I("black")) +
ggtitle(paste(data_label[j], " PC1 W1
Shoulder Ab/Adduction")) + xlab("Weight 1")+ xlim(c(-1,1)) + theme(plot.title = element_text(size=8))
PC1p[[2+(j-1)*4]]= ggplot(dataset,aes(dataset$PC1w2))+
geom_histogram(alpha=0.3,bins=20, fill=I(col[j]), col=I("black"))+
ggtitle(paste(data_label[j], " PC1 W2
Shoulder flexion/extension")) +xlab("Weight 2")+ xlim(c(-1,1))+ theme(plot.title = element_text(size=8))
PC1p[[3+(j-1)*4]]= ggplot(dataset,aes(dataset$PC1w3))+
geom_histogram(alpha=0.3,bins=20, fill=I(col[j]), col=I("black"))+
ggtitle(paste(data_label[j], " PC1 W3
Shoulder rotation")) + xlab("Weight 3")+xlim(c(-1,1)) + theme(plot.title = element_text(size=8))
PC1p[[4+(j-1)*4]]= ggplot(dataset,aes(dataset$PC1w4))+
geom_histogram(alpha=0.3,bins=20, fill=I(col[j]), col=I("black"))+
ggtitle(paste(data_label[j], " PC1 W4
Elbow")) + xlab("Weight 4")+ xlim(c(-1,1))+ theme(plot.title = element_text(size=8))
}
do.call("grid.arrange", c(PC1p, ncol=4,nrow=3))
最后一行,带有do.call和grid.array函数,返回以下错误
错误:美学必须为长度1或与数据相同(34(:x
如果所有单独的绘图都是从同一数据帧创建的,则运行相同的代码并返回一个网格排列绘图,不显示任何错误。
有趣的是,如果我在不使用任何for循环的情况下单独构建所有12个ggplot对象,然后使用grid.array(如下所示(,则会创建出很好的
p1=ggplot(Healthy,aes(Healthy$PC1w1))+
geom_histogram(alpha=0.3,bins=20, fill=I("blue"),col=I("black"))+
ggtitle("Healthy PC1 W1
Shoulder Ab/Adduction") + xlab("Weight 1")+ xlim(c(-1,1)) + theme(plot.title = element_text(size=8))
p2= ggplot(Healthy,aes(Healthy$PC1w2))+
geom_histogram(alpha=0.3,bins=20, fill=I("blue"), col=I("black"))+
ggtitle("Healthy PC1 W2
Shoulder flexion/extension") +xlab("Weight 2")+ xlim(c(-1,1))+ theme(plot.title = element_text(size=8))
p3= ggplot(Healthy,aes(Healthy$PC1w3))+
geom_histogram(alpha=0.3,bins=20, fill=I("blue"), col=I("black"))+
ggtitle("Healthy PC1 W3
Shoulder rotation") + xlab("Weight 3")+xlim(c(-1,1)) + theme(plot.title = element_text(size=8))
p4= ggplot(Healthy,aes(Healthy$PC1w4))+
geom_histogram(alpha=0.3,bins=20, fill=I("blue"), col=I("black"))+
ggtitle("Healthy PC1 W4
Elbow") + xlab("Weight 4")+ xlim(c(-1,1))+ theme(plot.title = element_text(size=8))
pre1=ggplot(patdata,aes(patdata$PC1w1))+
geom_histogram(alpha=0.3,bins=20, fill=I("red"),col=I("black"))+
ggtitle("Pre-therapy PC1 W1
Shoulder Ab/Adduction") + xlab("Weight 1")+ xlim(c(-1,1)) + theme(plot.title = element_text(size=8))
pre2= ggplot(patdata,aes(patdata$PC1w2))+
geom_histogram(alpha=0.3,bins=20, fill=I("red"), col=I("black"))+
ggtitle("Pre-therapy PC1 W2
Shoulder flexion/extension") +xlab("Weight 2")+ xlim(c(-1,1))+ theme(plot.title = element_text(size=8))
pre3= ggplot(patdata,aes(patdata$PC1w3))+
geom_histogram(alpha=0.3,bins=20, fill=I("red"), col=I("black"))+
ggtitle("Pre-therapy PC1 W2
Shoulder rotation") + xlab("Weight 3")+xlim(c(-1,1)) + theme(plot.title = element_text(size=8))
pre4= ggplot(patdata,aes(patdata$PC1w4))+
geom_histogram(alpha=0.3,bins=20, fill=I("red"), col=I("black"))+
ggtitle("Pre-therapy PC1 W4
Elbow") + xlab("Weight 4")+ xlim(c(-1,1))+ theme(plot.title = element_text(size=8))
Post1=ggplot(post_patdata,aes(post_patdata$PC1w1))+
geom_histogram(alpha=0.3,bins=20, fill=I("green"),col=I("black"))+
ggtitle("Post-therapy PC1 W1
Shoulder Ab/Adduction") + xlab("Weight 1")+ xlim(c(-1,1)) + theme(plot.title = element_text(size=8))
Post2= ggplot(post_patdata,aes(post_patdata$PC1w2))+
geom_histogram(alpha=0.3,bins=20, fill=I("green"), col=I("black"))+
ggtitle("Post-therapy PC1 W2
Shoulder flexion/extension") +xlab("Weight 2")+ xlim(c(-1,1))+ theme(plot.title = element_text(size=8))
Post3= ggplot(post_patdata,aes(post_patdata$PC1w3))+
geom_histogram(alpha=0.3,bins=20, fill=I("green"), col=I("black"))+
ggtitle("Post-therapy PC1 W2
Shoulder rotation") + xlab("Weight 3")+xlim(c(-1,1)) + theme(plot.title = element_text(size=8))
Post4= ggplot(post_patdata,aes(post_patdata$PC1w4))+
geom_histogram(alpha=0.3,bins=20, fill=I("green"), col=I("black"))+
ggtitle("Post-therapy PC1 W4
Elbow") + xlab("Weight 4")+ xlim(c(-1,1))+ theme(plot.title = element_text(size=8))
grid.arrange(p1,p2,p3,p4,pre1,pre2,pre3,pre4,Post1,Post2,Post3,Post4, ncol=4, nrow=3)
我不确定哪里出了问题。将非常感谢任何指导
一个很好的bug代码消除技巧是从一个更简单的案例开始,用不太重要的细节构建,直到实现了函数的所有部分。
当我在控制台中按原样运行您的代码时,由于您的数据帧列表,它将不会通过for循环。这应该是作为列表存储的数据的子集吗?
无论如何,我的猜测是,你的功能并没有产生你想象的那么多情节。你试过测试你列出的清单的长度吗?
在ggplot调用中删除对aes((中数据帧的引用似乎可以解决这个问题。
所以与其写
ggplot(dataset,aes(dataset$PC1w1)
写入
ggplot(dataset,aes(PC1w1)
对于每一个ggplot调用都会起作用。然而,我不太确定为什么要修复它,并从另一个博客得到了答案