For loop 如何使用ggplot2从for循环中的数据帧生成打印标题?
对于for循环生成的每个绘图,我想从我正在使用的数据框中为ggsave生成绘图标题和文件名 我想把月份写成一月、二月、三月等,而不是我的情节标题中的1、2、3。我怎样才能优雅地做到这一点 例如,如果我在1月份绘制A1和B1,我希望标题是2016年1月和下个月的A1和B1:2016年2月的A1和B1,依此类推。我希望每个月每个绘图的文件名都类似。对于标题,月份显示为1,而2016年则完全不显示。对于文件名,它可以按原样工作,但每次我更改AA och BB时都必须更改它,如果不必这样做,我会喜欢它 我还希望从我过滤的任何数据中生成此数据,因此,如果我过滤2016年1月的A1和B1,我希望将其转移到标题中,而不必每月都键入 我制作了一个数据帧,用于解决这个问题,真正的数据帧是巨大的,当然,我的R脚本非常长For loop 如何使用ggplot2从for循环中的数据帧生成打印标题?,for-loop,ggplot2,For Loop,Ggplot2,对于for循环生成的每个绘图,我想从我正在使用的数据框中为ggsave生成绘图标题和文件名 我想把月份写成一月、二月、三月等,而不是我的情节标题中的1、2、3。我怎样才能优雅地做到这一点 例如,如果我在1月份绘制A1和B1,我希望标题是2016年1月和下个月的A1和B1:2016年2月的A1和B1,依此类推。我希望每个月每个绘图的文件名都类似。对于标题,月份显示为1,而2016年则完全不显示。对于文件名,它可以按原样工作,但每次我更改AA och BB时都必须更改它,如果不必这样做,我会喜欢它
crs <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
Number <- 1:48
AA <- c(rep("A1", 12), rep("A2", 12), rep("A3", 12),rep("A4", 12))
BB <- c(rep("B4", 12), rep("B3", 12), rep("B2", 12),rep("B1", 12))
Long <- c(-46.25, -45.85,-45.85,-45.65, -45.983, -45.95,-45.85, -47.116,-47.1,-47.65, -47.116,-47.65,
-47.1,-47.633,-47.116,-47.65, -47.1, -47.65, -47.116, -47.666, -46.866, -47.116, -47.783, -47,
-46.25, -45.85, -45.85, -45.65, -45.983, -45.95, -45.85, -47.116, -47.1, -47.65, -47.116, -47.65
-47.1, -47.633, -47.116, -47.65, -47.1, -47.65, -47.116, -47.666, -46.866, -47.116, -47.783, -47, -47)
Lat <- c(46.55, 46.416, 46.533, 46.45, 46.433, 46.433, 46.45, 48.116, 48.083, 48.133, 48.1, 48.133,
48.1, 48.133, 48.116, 48.15, 48.1, 48.133, 48.1, 48.133, 48.133, 48.116, 48, 47.683,
46.55, 46.416, 46.533, 46.45, 46.433, 46.433, 46.45, 48.116, 48.083, 48.133, 48.1, 48.133,
48.1, 48.133, 48.116, 48.15, 48.1, 48.133, 48.1, 48.133, 48.133, 48.116, 48, 47.683)
End_month <- sample(1:12, size=48, replace = TRUE)
Category1 <- c(rep("Yes", 16), rep("No", 16), rep("Maybe", 16))
Category <- sample(x = Category1, size = 48)
Year<- c(rep("2016", 48))
dfAllData2 <- data.frame(Number, Long, Lat, End_month, Year, Category,
AA, BB )
#
newdata2 <- c("Lat", "Long", "End_month","Year", "AA", "BB", "Category" ) # select the column names of the columns in the plot
dfSubsets <- dfAllData2[newdata2]
# filter and create 2 layers of data----
Month_list <- (unique(dfSubsets$End_month))
for (i in seq_along(Months_list)){
dfFiltered1 <- dfSubsets %>%
filter(AA=="A1"& BB=="B1")
#create 2 sets of data
dfLayer1 <- dfFiltered1 %>%
filter(Category %in% c("Yes","No" ))
dfLayer2 <- dfFiltered1 %>%
filter(Category == "Maybe" )
#Plot ----
myPlot <- plot(dfFiltered1$Long, dfFiltered1$Lat)
ggplot(myPlot) +
geom_point(data = dfLayer1, aes(Long, Lat, color=Category, shape = Category), size = 2 ) + guides(size= FALSE)+
geom_point (data = dfLayer2, aes(Long, Lat, color=Category, shape = Category), size = 2 ) + guides(size= FALSE)
myPlot <- ggplot(myPlot) +
geom_point(data = dfLayer1, aes(Long, Lat, color=Category, shape = Category), size = 2 ) + guides(size= FALSE)+
geom_point (data = dfLayer2, aes(Long, Lat, color=Category, shape = Category), size = 2 ) + guides(size= FALSE)
print(myPlot + coord_cartesian(xlim = c(-43, -55), ylim = c(39, 49)) +
theme_bw()+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
plot.title = element_text(hjust = 0, face = "bold", size = 14),
legend.justification = c(0,1), legend.position = c(0,1), legend.background = element_rect(color = "black"))+
ggtitle("A1, and B1,", Month_list[i], "2016")+
labs(x = "Longitude", y = "Latitude"))
ggsave(paste("A1_B1_", Month_list[i],"_2016", ".png"))
}
试试这个。代码在所有月份循环。但我只包括前三个情节。此外:我还整理了代码 图书馆管理员 图书馆
谢谢你的回答,谢谢你整理我的代码,这很有帮助!我尝试了你的改进,但它每个月都会生成完全相同的图,所以我必须弄清楚它为什么会这样做。标题中的月份仍然是1、2、3,而不是1月、2月、3月。也许有办法解决这个问题吗?我们得到的是相同的图,因为我们总是使用相同的数据。所以我想过滤条件应该包括像End_month==i这样的内容。是的。当然,可以很容易地确定月份的名称。我去看看。编辑我的答案。我只是编辑了代码。现在在标题和文件名中都使用了month.name。第二我还为月末=I进行过滤。最后,循环中出现了bug。我们想要的是月表中的i,而不是月表中的i。使用后者的效果是“i”与月份数不对应。