Hi:
1. You need to simplify your plot call - the calls to theme() are
unnecessary for the purposes of your question.
2. Your code got mashed together likely because you sent your mail in
HTML; therefore, it wasn't easy to read.
Complaints aside, the biggest programming problem was to combine
normal and italic fonts in the title across two lines and avoid
getting a large space between the end of the first text chunk and the
italicized text. From your end, you have to distinguish the contents
of an atomic vector from their respective indices/locations. Below is
one way to do what I think you requested. I made up a fake data set to
generate multiple species; you could extend the idea to species-park
combinations if that is of interest.
# Fake data set - year is numeric rather than factor, so a group
# aesthetic for it is unnecessary.
DF <- data.frame(
species = factor(rep(paste0("species_", LETTERS[1:3]), each = 10)),
rcp = c("group1", "group2"),
year = rep(2011:2015, each = 2),
MEAN = runif(30, 0.3, 0.6))
library(ggplot2)
# define an atomic vector of species names
species_names <- as.character(unique(DF$species))
# A function to create a plot for an individual species. It takes a
# data frame DF and a character object species_name as formal arguments
plotfun <- function(DF, species_name)
{
d <- DF[DF$species == species_name, ] # get the data subset
# Manufacture an expression that creates a two-line title
my_title <- bquote(atop(paste("Mean Suitability for ",
italic(.(species_name)), " by Year"),
"Anonymous National Park"))
# create and return the ggplot
ggplot(d, aes(x = year, y = MEAN, colour = rcp)) +
geom_point() +
geom_line() +
labs(title = my_title, x = "Projection Year",
y="Mean Suitability\nValue", colour = "factor")
}
# Apply the above function to each species, return a list of ggplot objects
L <- lapply(species_names,
function(x) plotfun(DF, x))
# Cycle through the list of plots and save them to individual files
for(i in seq_along(species_names))
{
filename <- paste(species_names[i], "png", sep = ".")
ggsave(filename, L[[i]], width = 6, height = 2.5)
}
Creating axis titles and main titles in a plot can sometimes be a
challenge, particularly when one needs to insert non-standard text
such as mathematics or text in a modified font. The challenge here was
to create a main title with two lines that mixes normal and italic
text. \n works when given a text string without 'embellishments', but
unexpected things can happen if your input is an expression rather
than a string. I made two adjustments: (i) I used atop() because it
can take an expression or a string as an argument; (ii) I modified the
title so that the italicized text was on the first line inside an
expression().
The code above cycles through each species name, first taking the
appropriate data subset and then producing and returning a plot with a
two-line main title. The lapply() call produces a list of plots, one
per species. The loop at the end cycles through the list, saving each
plot to a file in png format. I find it useful to save the plots in a
list in case you need to reference them later. If you don't want the
list of plots, assign the ggplot() output to an object, say p, inside
plotfun() and make its last line
ggsave(p, file = paste(species_name, "png", sep = ".", width = 6, height = 2.5))
HTH,
Dennis