I would like to generate plot titles and filenames for ggsave from the data frame that I am using, for every plot that the for loop generates.
And I would like the months to be written as January, February, March etc rather than 1, 2, 3 in my plot titles. How can I do this in an elegant way?
For example, if I plot A1 and B1 for January, I would like the title to be A1 and B1, January 2016 and for the next month : A1 and B1 for February 2016 and so on. and I want something similar for the filename of each plot every month. For the title the months is shown as "1" and the year 2016 is not shown at all. For the filename it works as it is but I have to change it every time I change the AA och BB, and I would love it if I didn't have to do that.
I would also want this to be generated from whatever data i filter, so if I filter A1 and B1 for January 2016 I want it to transfer into the title without me having to type it in for every month.
I made up a dataframe that will do for the purpose of this question (the real data frame is huge, of course, and my R script is very long).
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"))
}


