我正在尝试使用grid.arrange和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.arrange函数的最后一行返回以下错误
错误:美学必须为长度1或与数据相同 (34):x
相同的代码运行并返回一个grid.arrange绘图,如果没有所有错误,则如果所有单个绘图都是从同一数据帧创建的。
有趣的是,如果我在不使用任何for循环的情况下分别构造所有12个ggplot对象,然后使用grid.arrange(如下所示),则可以很好地创建该图
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)
我不确定我要去哪里。任何指导将不胜感激
答案 0 :(得分:0)
调试代码的一个好技巧是从一个简单的案例开始,以不太重要的细节开始构建,直到实现功能的所有部分为止。
当我在控制台中按原样运行代码时,由于您的数据帧列表,它不会通过for循环进行。这应该是作为列表存储的数据的子集吗?
无论如何,我的猜测是您的函数产生的绘图不如您想象的那么多。您是否尝试过测试列表输出的长度?
答案 1 :(得分:0)
在ggplot调用内的aes()中删除对数据帧的引用似乎可以解决此问题。
所以不要写
ggplot(dataset,aes(dataset$PC1w1)
写作
ggplot(dataset,aes(PC1w1)
对于每个ggplot调用,都能解决问题。 我不太确定为什么可以解决这个问题,但是从另一个博客那里得到了答案