这个问题与this one和this one的不同之处在于我得到了一个传说。但是,图例中的值仅带有彩色边框,而不是整个区域。 我很确定我忘记指定了一个简单的论点,但是我正在寻找它大约2个小时,然后慢慢地发疯了。也许你可以帮忙。
首先,是一个可复制的示例:
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)
如您所见,该函数按要求工作(看起来并不好,但毕竟只是一个示例。由于数据是随机生成的,因此通常看起来并非如此),因此看起来不太好。 BUUUT,看看传奇!!!
我不知道如何填充“方块”,老实说,我不知道还有什么其他办法,因此,我们将不胜感激!提前致谢!!!