this question is different from this one and this one in the sense that I get a legend. However, the values in the legend only have coloured borders, and not the entire area. I am fairly sure there is an easy argument I forgot to specify, but I am looking for it for about 2 hours now and am slowly going crazy. Maybe you can help.
First, a reproducable example:
library(ggplot2)
set.seed(1234)
#this only generates coordinates, in my case 72 latitude and 144 longitude ones. ignore it if you want
df <- data.frame(lon = rep(1:144/144*360-180,72), lat = rep(1:72/72*180-90, each = 144), val = runif(72*144) )
world <- map_data("world")
# This function is supposed to colour all points where the val columns is less than a specified value in blue, and the others in red.
#'@param cutoff a cutoff value, where all values less than the value are plotted in blue and all coefficients greater than it are plotted in red
#'@return A plot object of a map of the world where the color indicates the value
#'@export
plot_with_cutoff = function(df, cutoff = quantile(df$val, 0.05)){
df$indicator <- as.factor(ifelse(df$val<cutoff,0,1))
plot <- ggplot() + geom_tile(data = df, mapping = aes(x = lon, y = lat, color = indicator)) +
coord_fixed(1.4)+ scale_color_manual("Higher or lower than 5% quantile", values = c("blue","red")) +
geom_polygon(data = world, aes(x=long, y = lat, group = group), fill = NA, color = "black")
return(plot)
}
plot_with_cutoff(df)
As you can see, the function works as requested (it's not nice to look at, but only an example after all. It's not nice to look at because the data is randomly generated, which it ususally isn't). BUUUT, LOOK AT THE LEGEND!!!
I don't know how to get the "squares" to be filled, and I honestly have no idea what else to do, so any help is greatly appreciated! Thanks in Advance!!!