【发布时间】:2020-02-08 11:43:20
【问题描述】:
第一次提问所以如果我错过了任何重要的细节,请提前道歉。我还是 R 的新手,但随着我了解更多,我会尝试为自己做出贡献。同时希望有人可以提供帮助。
我有一个 for 循环,可以从我的数据中生成多个图。情节本身是完美的,但我正在努力将情节 1 到 4 添加到单个图像中。
如果我使用plot(),我可以使用par(mfrow - c(1,2,3,4),但由于我要向每个绘图添加多个数据框,即在ggplot 中添加多个geom_point(),我认为我不能这样做。
我的数据看起来像这样,但要长得多,N01、N02 等线的多个数据点,我正在生成一个新图,其中包含 N01、N02 等的数据,叠加了 WT1 和 WT2 点。
我想我可能需要将循环的每次迭代保存到新变量中,然后使用multiplot() 来获得我想要的结果。
如果我想出答案,我会发布,但如果有人击败我,请提前感谢您。
N01 N01.01 0.0 7693
2 N01 N01.02 2.0 3404
3 N01 N01.03 2.0 3404
4 N01 N01.04 1.0 6395
5 N01 N01.05 1.0 5171
6 N01 N01.06 2.0 6001
7 N01 N01.07 1.0 6671
8 N01 N01.08 1.5 6700
9 N01 N01.09 1.0 9060
10 N04 N04.01 2.0 6857
11 N04 N04.02 4.0 10378
setwd ("C:/Users/HP/Desktop/Studio R/Patty")
df <- read.csv("Data/Raw V2.csv")
#need to create a new data frame with a new coluum taking information from another column
df2 <- split(df, df$ï..ID)
x1 <- df2$WT1[,3:4] #new var for the wild type that wants plotting on all the charts
x2 <- df2$WT6[,3:4]
plot_list <- list() #creates an empty list to save the plots in
for (i in 1:length(df2)){
z1 <- df2[[i]][,3:4]#new var from the data fram i amount of times using data from column 3 to 4
title <- (df2[[i]][,1][1]) #used later on to add the title variable in the legend
par(mar=c(4,4,1,1), mfrow=c(2,2))
f <-
ggplot(data=z1, aes(x=copies, y=leaf_area)) +
geom_point(aes(col="black"))+ #creates the plots of i
geom_point(data=x1, aes(col="blue"))+ #adds the wt to all plots, aes neeeded to create legend
geom_point(data=x2, aes(col="red"))+
theme(panel.background = element_blank(), axis.line = element_line(colour = "black"),
panel.grid.minor = element_line())+
scale_color_identity(name = "", breaks = c("black", "blue", "red"), labels = c(paste0("(",title,")"),("(WT1)"),("(WT6)")), guide = "legend")+
theme(legend.position = c(0.95, 0.95), legend.justification = c("right", "top"), legend.direction = "horizontal")+
scale_x_continuous(limits = c(0, 4)) + #standarsises the axis
scale_y_continuous(limits = c(0, 12000)) +
xlab(bquote("Number of Inserts"))+
ylab(bquote("Leaf Area (mm'^2')"))+
ggtitle(df2[[i]][,1][1])
plot_list[[i]] <- f
list2env(plot_list[[i]], envir = globalenv()) #send the list to the global environment
}
在理想的世界中,这会起作用:
for (i in plot_list){
par(mfrow = c(2,2))
plot(i)
}
【问题讨论】: