在下面的示例中,我想使用蓝色配色(从最接近100的浅蓝色到最大的深蓝色)为大于100的值着色,并在暖色范围(从黄色为黄色)中为小于100的值着色表示最接近100的红色。请参阅下面的颜色范围示例。有人可以帮我吗?我尝试了几种不同的方法(包括下面的一种方法),但是没有成功。非常感谢!
#library
library(raster)
library(ggplot2)
library(maptools)
data("wrld_simpl")
#sample raster
r <- raster(ncol=36, nrow=18)
r[] <- (-ncell(r)/2+1):(ncell(r)/2)
plot(r)
var_df <- as.data.frame(rasterToPoints(r))
#plotting
p <- ggplot()
p <- p + geom_raster(data = var_df , aes(x = x, y = y, fill = layer))
p <- p + coord_equal()
p <- p + scale_fill_gradient2(low = muted("red"), mid = "white",
high = muted("blue"), midpoint = 100)
p
答案 0 :(得分:3)
嗯...所以,您实际上是否希望它精确地在layer = 100处分开?
如果是这样,
#plotting
p <- ggplot()
p <- p + geom_raster(data = var_df , aes(x = x, y = y, fill = layer))
p <- p + coord_equal()
p <- p + scale_fill_gradientn(
colours=c("red", "yellow", "skyblue", "darkblue"),
values = rescale(c(min(var_df$layer),
100,
100.01,
max(var_df$layer))))
答案 1 :(得分:1)
一个选项可能是个人ifelse
功能来设置颜色
colour_func <- function(x){
ifelse(x$x > 150, 'darkblue',
ifelse(x$x > 130, 'cyan4',
ifelse(x$x > 110, 'cadetblue3',
ifelse(x$x > 100, 'cadetblue',
ifelse(x$x > 90, 'red',
ifelse(x$x > 60, 'darkorange3',
ifelse(x$x > 40, 'darkorange',
ifelse(x$x > 20, 'goldenrod2', "gold"))))))))
}
> #plotting
> p <- ggplot()
> p <- p + geom_raster(data = var_df , aes(x = x, y = y, fill = layer))
> p <- p + coord_equal()
> p <- p + scale_fill_gradientn(colours = colour_func(var_df))
> p