Jim*_*BOB 0 plot for-loop r ggplot2
第一次提问,如有遗漏,请多多包涵。我对 R 还很陌生,但随着我了解更多,我会尝试贡献自己的力量。同时希望有人可以提供帮助。
\n\n我有一个 for 循环,可以根据我的数据生成多个图。这些图本身很完美,但是我正在努力将图 1 到 4 添加到单个图像中。
\n\n如果我使用plot()我可以使用par(mfrow - c(1,2,3,4),但当我向每个图添加多个数据框(即geom_point()ggplot 中的多个数据框)时,我认为我无法做到这一点。
我的数据看起来像这样,但更长,N01、N02 等线有多个数据点,我正在使用 N01、N02 等的数据生成一个新图,并覆盖 WT1 和 WT2 点。
\n\n我想我可能需要将循环的每次迭代保存到新变量中,然后使用它multiplot()来获得我想要的结果。
如果我想出答案,我会发布,但如果有人比我先找到答案,请提前谢谢您。
\n\nN01 N01.01 0.0 7693\n2 N01 N01.02 2.0 3404\n3 N01 N01.03 2.0 3404\n4 N01 N01.04 1.0 6395\n5 N01 N01.05 1.0 5171\n6 N01 N01.06 2.0 6001\n7 N01 N01.07 1.0 6671\n8 N01 N01.08 1.5 6700\n9 N01 N01.09 1.0 9060\n10 N04 N04.01 2.0 6857\n11 N04 N04.02 4.0 10378\n\nRun Code Online (Sandbox Code Playgroud)\n\nsetwd ("C:/Users/HP/Desktop/Studio R/Patty")\ndf <- read.csv("Data/Raw V2.csv")\n\n#need to create a new data frame with a new coluum taking information from another column\ndf2 <- split(df, df$\xc3\xaf..ID)\nx1 <- df2$WT1[,3:4] #new var for the wild type that wants plotting on all the charts\nx2 <- df2$WT6[,3:4]\n\nplot_list <- list() #creates an empty list to save the plots in\n\n\nfor (i in 1:length(df2)){\n z1 <- df2[[i]][,3:4]#new var from the data fram i amount of times using data from column 3 to 4\n title <- (df2[[i]][,1][1]) #used later on to add the title variable in the legend\n par(mar=c(4,4,1,1), mfrow=c(2,2))\n f <- \n ggplot(data=z1, aes(x=copies, y=leaf_area)) + \n geom_point(aes(col="black"))+ #creates the plots of i\n geom_point(data=x1, aes(col="blue"))+ #adds the wt to all plots, aes neeeded to create legend\n geom_point(data=x2, aes(col="red"))+\n theme(panel.background = element_blank(), axis.line = element_line(colour = "black"),\n panel.grid.minor = element_line())+\n scale_color_identity(name = "", breaks = c("black", "blue", "red"), labels = c(paste0("(",title,")"),("(WT1)"),("(WT6)")), guide = "legend")+\n theme(legend.position = c(0.95, 0.95), legend.justification = c("right", "top"), legend.direction = "horizontal")+\n scale_x_continuous(limits = c(0, 4)) + #standarsises the axis\n scale_y_continuous(limits = c(0, 12000)) +\n xlab(bquote("Number of Inserts"))+\n ylab(bquote("Leaf Area (mm\'^2\')"))+\n ggtitle(df2[[i]][,1][1])\n plot_list[[i]] <- f\n list2env(plot_list[[i]], envir = globalenv()) #send the list to the global environment\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在理想的世界中,这会起作用:
\n\nfor (i in plot_list){\n par(mfrow = c(2,2))\n plot(i)\n}\nRun Code Online (Sandbox Code Playgroud)\n
您试图使用par(mfrow=...),并且这只适用于基本 R 图。对于ggplot,如果您喜欢类似的东西,也许可以使用 gridExtra 因为您已经有了一个绘图列表。您可以看到下面的示例iris
另外作为评论,很可能您不需要使用,list2env因为您已经分配了它
library(ggplot2)
library(gridExtra)
plot_list <- list()
df <- split(iris,iris$Species)
for(i in seq_along(df)){
plot_list[[i]] <- ggplot(df[[i]],aes(x=Sepal.Length,y=Sepal.Width))+
geom_point()+
ggtitle(names(df)[i])
}
grid.arrange(grobs=plot_list,ncol=2)
Run Code Online (Sandbox Code Playgroud)
我发现有一个你总是想要比较的参考。我将模拟一些看起来像你的数据的东西:
set.seed(100)
WT1 <- data.frame(Sepal.Length=seq(4,6.5,length.out=50),
Sepal.Width=seq(1.5,3,length.out=50)+rnorm(50,0.5,0.2),Species="WT1")
WT2 <- data.frame(Sepal.Length=seq(6,8,length.out=50),
Sepal.Width=seq(2,4.5,length.out=50)+rnorm(50,0.5,0.2),Species="WT2")
df <- rbind(iris[,c("Sepal.Length","Sepal.Width","Species")],WT1,WT2)
colnames(df)[3] <- "ID"
Run Code Online (Sandbox Code Playgroud)
现在我们绘制:
# separate the two datasets you want:
obs <- droplevels(subset(df,!ID %in% c("WT1","WT2")))
ref <- droplevels(subset(df,ID %in% c("WT1","WT2")))
plot_list <- list()
for(i in unique(obs$ID)){
thisDF <- rbind(subset(obs,ID==i),ref)
g <- ggplot(thisDF,aes(x=Sepal.Length,y=Sepal.Width,col=ID))+
geom_point() + theme(legend.position = c(0.9,0.9),
legend.justification = c("right", "top"),
legend.direction = "horizontal")+ggtitle(i)
plot_list[[i]] <- g
}
grid.arrange(grobs=plot_list,ncol=2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3384 次 |
| 最近记录: |