ggplot | Легенда не заполнена - PullRequest
0 голосов
/ 01 сентября 2018

этот вопрос отличается от этого и этого в том смысле, что я получаю легенду. Однако значения в легенде имеют только цветные рамки, а не всю область. Я вполне уверен, что есть простой аргумент, который я забыл указать, но я ищу его около 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, СМОТРИТЕ НА ЛЕГЕНДУ !!!
Я не знаю, как заполнить «квадраты», и я, честно говоря, понятия не имею, что еще делать, поэтому любая помощь очень ценится! Заранее спасибо!!! this is the result

...