The following should pretty much reproduce what I am trying and works:
library(ggplot2)
library(mapdata)
latitude <- seq(from = 48, to = -47.5, by = -1.)
longitude <- seq(from = -150, to = -70.5, by = 1.)
temp <- expand.grid( x = longitude, y = latitude)
xlim <- c(temp$x[1], temp$x[length(temp$x)])
ylim <- c(temp$y[length(temp$y)], temp$y[1])
longitude <- temp$x
latitude <- temp$y
no_loc <- length(longitude)
spatial_amp <- rnorm(no_loc,mean=0,sd=1)
spatial_amp_frame <- data.frame(amplitude = spatial_amp, longitude = longitude, latitude = latitude)
w <- map_data("worldHires", ylim = ylim, xlim = xlim)
amplitude_map <- ggplot(data = spatial_amp_frame, aes_string(x = names(spatial_amp_frame)[2], y = names(spatial_amp_frame)[3], fill = names(spatial_amp_frame)[1])) +
geom_raster(interpolate = FALSE, na.rm = TRUE) +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "gray50") +
theme_bw() + ylab("latitude") + xlab("longitude") +
coord_fixed(1.3, xlim = xlim, ylim = ylim)
The following would be the same but doesn't get the fill correctly:
x_var = quo(names(spatial_amp_frame)[2])
y_var = quo(names(spatial_amp_frame)[3])
fill_var = quo(names(spatial_amp_frame)[1])
myplot <- ggplot(data = spatial_amp_frame, aes(!!x_var, !!y_var, fill = !!fill_var) )+
geom_raster(interpolate = FALSE, na.rm = TRUE) +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "gray50") +
theme_bw() + ylab("latitude") + xlab("longitude") +
coord_fixed(1.3, xlim = xlim, ylim = ylim)
In fact I am generating a lot of maps, and have a function that helps with names. At present it is:
make_map <- function(my_frame, xlim, ylim, my_color, title = NA, limits = NA) {
require(ggplot2)
require(mapdata)
w <- map_data("worldHires", ylim = ylim, xlim = xlim)
myplot <- ggplot(data = my_frame, aes_string(x = names(my_frame)[2], y = names(my_frame)[3], fill = names(my_frame)[1])) +
geom_raster(interpolate = FALSE, na.rm = TRUE) +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "gray50") +
theme_bw() + ylab("latitude") + xlab("longitude") +
coord_fixed(1.3, xlim = xlim, ylim = ylim)
if (!is.na(limits)) {
myplot <- myplot + scale_fill_gradientn(colours = my_color, limits = limits, na.value = NA)
} else{
myplot <- myplot + scale_fill_gradientn(colours = my_color, na.value = NA)
}
if (!is.na(title)) {
myplot <- myplot + ggtitle(title)
}
myplot
}
and what I have tried is:
make_map1 <- function(my_frame, xlim, ylim, my_color, title = NA, limits = NA) {
require(ggplot2)
require(mapdata)
x_var = quo(names(my_frame)[2])
y_var = quo(names(my_frame)[3])
fill_var = quo(names(my_frame)[1])
w <- map_data("worldHires", ylim = ylim, xlim = xlim)
myplot <- ggplot(data = my_frame, aes(!!x_var, !!y_var, fill = !!fill_var)) +
geom_raster(interpolate = FALSE, na.rm = TRUE) +
geom_polygon(data = w, aes(x = long, y = lat, group = group), fill = "gray50") +
theme_bw() + ylab("latitude") + xlab("longitude") +
coord_fixed(1.3, xlim = xlim, ylim = ylim)
if (!is.na(limits)) {
myplot <- myplot + scale_fill_gradientn(colours = my_color, limits = limits, na.value = NA)
} else{
myplot <- myplot + scale_fill_gradientn(colours = my_color, na.value = NA)
}
if (!is.na(title)) {
myplot <- myplot + ggtitle(title)
}
myplot
}