R 在ggplot2中对多个绘图使用循环
我看过这些问题,但它们似乎解决了一个不同的问题,与我的案例无关。通常,我使用这段代码来生成好的多个图形,这些图形在我使用这段代码时非常有用R 在ggplot2中对多个绘图使用循环,r,loops,plot,ggplot2,R,Loops,Plot,Ggplot2,我看过这些问题,但它们似乎解决了一个不同的问题,与我的案例无关。通常,我使用这段代码来生成好的多个图形,这些图形在我使用这段代码时非常有用 library(ggplot2) library(maptools) library (rgdal) library (rgeos) library(export) library(ReporteRs) library(ReporteRsjars) library(rtable) library(xtable) library(taRifx) library
library(ggplot2)
library(maptools)
library (rgdal)
library (rgeos)
library(export)
library(ReporteRs)
library(ReporteRsjars)
library(rtable)
library(xtable)
library(taRifx)
library(devtools)
library(tidyr)
library(dplyr)
#Tidying up data from wide to long format
plots <- read.delim("withoutPG3,23,24.txt")
head(plots,5)
################ SUBSETTING DATA INTO 10 MANAGABLE PLOTS ###########################
###### PLOT 1
plot1<- plots %>%
select(1:5,Ammonia, Asterorotalia, Bolivina, Bulimina, Cancris, Challengerella, Cibicides, Elphidium, Hanzawaia, Nonion)%>%
gather(genus,`Relative Abundance (%)`, Ammonia, Asterorotalia, Bolivina, Bulimina, Cancris, Challengerella, Cibicides, Elphidium, Hanzawaia, Nonion)
############################################################################################################
## Loading the Persian Gulf shape fill referred to as iho
PG <- readShapePoly("iho.shp")
PG <- readOGR(dsn = ".", "iho")
# convert to dataframe
AG <- fortify(PG)
#####################################################################################################
background=ggplot()+
geom_polygon(data=AG, aes(long, lat, group = group, fill = hole),
colour = alpha("black", 0.3), show.legend = FALSE)+
scale_fill_manual(values = c("grey80", "white")) +
#######
geom_point(data=plot1, aes(x=Longitude, y=Latitude, size=`Relative Abundance (%)`,
color="Red"))+
scale_size_area("`Relative Abundance (%)`")+
facet_wrap(~genus, ncol=4)+
scale_x_continuous("", breaks=NULL) +
scale_y_continuous("", breaks=NULL)+
annotate(geom="text", x=53, y=28,label="Iran",color="black", size = 3)+
annotate(geom="text", x=48.5, y=26,label="Saudi \nArabia",color="black", size = 3)+
annotate(geom="text", x=50.6, y=26,label="Bahrain",color="black", size = 3)+
annotate(geom="text", x=51.2, y=25.3,label="Qatar",color="black", size = 3)+
annotate(geom="text", x=56, y=24.5,label="United Arab \nEmirates",color="black", size = 3)+
annotate(geom="text", x=57, y=25.7,label="Strait of \nHormuz",color="black", size = 3)
#
#
background=background +theme_bw()+
ggtitle("PERSIAN GULF")
###
background=background +
theme(plot.title = element_text(face="bold", size=15, vjust=1),
axis.title.y = element_text(face="bold", size=15),
axis.title.x =element_blank(),
axis.text.x = element_blank(),
legend.position = c(.7, .15),
#legend.position = "top",
legend.title = element_text(colour="black", size=15, face="bold"),
legend.text = element_text(colour="black", size=10, face="bold"))
#
background
graph2ppt(file="finalplots.pptx",aspectr=1.3)
下面是dputheadplot1和10,此处提供了shapefile
首先,让我为这篇冗长的文章道歉,我想提供一个尽可能重复的例子。我会很感激能在这件事上得到的任何帮助。谢谢这将把一系列绘图保存为png绘图。我已经修改了你们的dput数据,所以有两个属,因此有两个图。我不确定这个功能的用途
library(ggplot2)
library(rgdal)
library(rgeos)
library(maptools)
# Loading the Persian Gulf shapefile
PG <- readShapePoly("iho.shp")
PG <- readOGR(dsn = ".", "iho")
# convert to dataframe
AG <- fortify(PG)
# Modify your dput data so that there are two genera
plot1$genus = rep(c("AAA", "BBB"), 1)
# create list of genus in data to loop over
foram_list <- unique(plot1$genus)
# create for loop to produce ggplot2 graphs
for (i in seq_along(foram_list)) {
df = subset(plot1, genus == foram_list[i])
# create plot for each genus in plot1
forams <- ggplot() +
geom_polygon(data=AG, aes(long, lat, group = group, fill = hole),
colour = alpha("black", 0.3), show.legend = FALSE) +
scale_fill_manual(values = c("grey80", "white")) +
########
geom_point(data = df,
aes(x=Longitude, y=Latitude, size=`Relative Abundance (%)`,
color="Red")) +
scale_size_area("`Relative Abundance (%)`")+
scale_x_continuous("", breaks=NULL) +
scale_y_continuous("", breaks=NULL)+
theme_minimal() +
ggtitle(foram_list[i])
png(paste0(foram_list[i], ".png"), 4, 4, "in", res = 100)
print(forams)
dev.off()
}
或者:如果要保存ggplot对象,我会将它们保存在命名列表中
# create list of genus in data to loop over
foram_list <- unique(plot1$genus)
# Initialise the list
forams = list()
# create for loop to produce ggplot2 graphs
for (i in seq_along(foram_list)) {
df = subset(plot1, genus == foram_list[i])
# create plot for each genus in plot1
forams[[i]] <- ggplot() +
geom_polygon(data=AG, aes(long, lat, group = group, fill = hole),
colour = alpha("black", 0.3), show.legend = FALSE) +
scale_fill_manual(values = c("grey80", "white")) +
geom_point(data = df,
aes(x=Longitude, y=Latitude, size=`Relative Abundance (%)`,
color="Red")) +
scale_size_area("`Relative Abundance (%)`")+
scale_x_continuous("", breaks=NULL) +
scale_y_continuous("", breaks=NULL)+
theme_minimal() +
ggtitle(foram_list[i])
}
names(forams) = foram_list
forams[["AAA"]]
forams[["BBB"]]
这将把一系列绘图保存为png绘图。我已经修改了你们的dput数据,所以有两个属,因此有两个图。我不确定这个功能的用途
library(ggplot2)
library(rgdal)
library(rgeos)
library(maptools)
# Loading the Persian Gulf shapefile
PG <- readShapePoly("iho.shp")
PG <- readOGR(dsn = ".", "iho")
# convert to dataframe
AG <- fortify(PG)
# Modify your dput data so that there are two genera
plot1$genus = rep(c("AAA", "BBB"), 1)
# create list of genus in data to loop over
foram_list <- unique(plot1$genus)
# create for loop to produce ggplot2 graphs
for (i in seq_along(foram_list)) {
df = subset(plot1, genus == foram_list[i])
# create plot for each genus in plot1
forams <- ggplot() +
geom_polygon(data=AG, aes(long, lat, group = group, fill = hole),
colour = alpha("black", 0.3), show.legend = FALSE) +
scale_fill_manual(values = c("grey80", "white")) +
########
geom_point(data = df,
aes(x=Longitude, y=Latitude, size=`Relative Abundance (%)`,
color="Red")) +
scale_size_area("`Relative Abundance (%)`")+
scale_x_continuous("", breaks=NULL) +
scale_y_continuous("", breaks=NULL)+
theme_minimal() +
ggtitle(foram_list[i])
png(paste0(foram_list[i], ".png"), 4, 4, "in", res = 100)
print(forams)
dev.off()
}
或者:如果要保存ggplot对象,我会将它们保存在命名列表中
# create list of genus in data to loop over
foram_list <- unique(plot1$genus)
# Initialise the list
forams = list()
# create for loop to produce ggplot2 graphs
for (i in seq_along(foram_list)) {
df = subset(plot1, genus == foram_list[i])
# create plot for each genus in plot1
forams[[i]] <- ggplot() +
geom_polygon(data=AG, aes(long, lat, group = group, fill = hole),
colour = alpha("black", 0.3), show.legend = FALSE) +
scale_fill_manual(values = c("grey80", "white")) +
geom_point(data = df,
aes(x=Longitude, y=Latitude, size=`Relative Abundance (%)`,
color="Red")) +
scale_size_area("`Relative Abundance (%)`")+
scale_x_continuous("", breaks=NULL) +
scale_y_continuous("", breaks=NULL)+
theme_minimal() +
ggtitle(foram_list[i])
}
names(forams) = foram_list
forams[["AAA"]]
forams[["BBB"]]
这是我的最后一段代码,以防有人发现它很有用,情节是这样的
这是我的最后一段代码,以防有人发现它很有用,情节是这样的
如果我没弄错你的问题,这可能是你的问题的副本。ggsave对您不起作用吗?实际上,您的代码是不可复制的。首先,需要加载相当多的包。其次,我们无法访问withoutPG3,23,24.txt,因此无法运行第一个代码段。第三,dput数据只包含一个属,因此对foram.graph函数的任何测试都将受到限制。@steveb,这不是同一个问题@Sandy,我觉得最有用的信息是plot1数据,因为它太多了,我觉得最好只显示前10行,但你的观察结果会被记录下来。如果我正确理解你的问题,这可能是的副本。ggsave对您不起作用吗?实际上,您的代码是不可复制的。首先,需要加载相当多的包。其次,我们无法访问withoutPG3,23,24.txt,因此无法运行第一个代码段。第三,dput数据只包含一个属,因此对foram.graph函数的任何测试都将受到限制。@steveb,这不是同一个问题@Sandy,我觉得最有用的信息是plot1数据,因为它太多了,我觉得最好只显示前10行,但你的观察结果会被记录下来。你的第一个答案有效。。。我无法让第二部分工作。我对第一个答案做了一些修改,以便将其另存为ppt的对象…您的第一个答案有效。。。我无法让第二部分工作。我修改了第一个答案,以便将其另存为ppt的对象。。。
library(ggplot2)
library(maptools)
library (rgdal)
library (rgeos)
library(export)
library(ReporteRs)
library(ReporteRsjars)
library(rtable)
library(xtable)
library(taRifx)
library(tidyr)
library(dplyr)
#########
# create list of genus in data to loop over
foram_list <- unique(plot1$genus)
# create for loop to produce ggplot2 graphs
for (i in seq_along(foram_list)) {
df = subset(plot1, genus == foram_list[i])
# create plot for each genus in plot1
forams <- ggplot() +
geom_polygon(data=AG, aes(long, lat, group = group, fill = hole),
colour = alpha("black", 0.3), show.legend = FALSE) +
scale_fill_manual(values = c("grey80", "white")) +
########
geom_point(data = df,
aes(x=Longitude, y=Latitude, size=`Relative Abundance (%)`,
color="Red")) +
#scale_radius()+
scale_size_area("`Relative Abundance (%)`")+
scale_x_continuous("", breaks=NULL) +
scale_y_continuous("", breaks=NULL)+
theme_minimal() +
annotate(geom="text", x=53, y=28,label="Iran",color="black", size = 5)+
annotate(geom="text", x=48.5, y=26,label="Saudi \nArabia",color="black", size = 5)+
annotate(geom="text", x=50.6, y=26,label="Bahrain",color="black", size = 5)+
annotate(geom="text", x=51.2, y=25.3,label="Qatar",color="black", size = 5)+
annotate(geom="text", x=56, y=24.5,label="United Arab \nEmirates",color="black", size = 5)+
annotate(geom="text", x=57, y=25.7,label="Strait of \nHormuz",color="black", size = 5)+
ggtitle(foram_list[i])
#
forams=forams +
theme(plot.title = element_text(face="italic", size=15, vjust=1),
axis.title.y = element_text(family = "Trebuchet MS", color="#666666", face="bold.italic", size=30),
#family = "Trebuchet MS", color="#666666", face="bold",
axis.title.x =element_blank(),
axis.text.x = element_blank(),
legend.position = c(.8, .8),
#legend.position = "top",
legend.title = element_text(colour="black", size=15, face="bold"),
legend.text = element_text(colour="black", size=10, face="bold"))
#
graph2ppt(x=forams,file="forams.pptx",append=TRUE)
dev.off()
}