如何根据 R/ggplot2 中的特定值范围创建图例(连续颜色条)?

How to create legend (continuous colourbar) based on specific range of values in R/ggplot2?

我有一个名为 myKrige_new 的数据框,其中包含一些经纬度插值。您可以从 HERE 下载。我在 R 中使用 ggplot2 包在国家地图的特定区域绘制了这个值,我得到了这个图

但我希望我的情节的图例(颜色条)像下面的图例。

在我这里的数据集中,数据 (pred) 的范围是 72 到 257。但是我希望我的图例显示值 0 到 200,因为与其他图比较的原因虽然这里没有低于 72 的值。

所以,我想像上面的图例一样使用 20 种不同的颜色,这意味着图例的最后一个框将包含关于值大于 200 的颜色。我使用了 scale_fill_gradientn 函数,但它没有用。我花了几天时间在 R 中找到一些选项,但没有成功。任何形式的帮助都将不胜感激。

R代码:

library(scales)
library(ggplot2)

myKrige_new <- read.csv ("myKrige_new.csv")

range(myKrige_new$LON) 
range(myKrige_new$LAT)

#Original skorea data transformed the same was as myKrige_new
skorea1 <- getData("GADM", country= "KOR", level=1)
skorea1 <- fortify(skorea1)  
myKorea1 <- data.frame(skorea1)

###############
ggplot()+ 
  theme_minimal() +
  #SOLUTION 1:
  #geom_tile(data = myKrige_new, aes(x= LON, y= LAT, fill = pred)) +

  #SOLUTION 2: Uncomment the line(s) below:
  #geom_point(data = myKrige_new, aes(x= LON, y= LAT, fill = pred),
  #shape=22, size=8, colour=NA)+ 

  #Solution 3
  stat_summary_2d(data=myKrige_new, aes(x = LON, y = LAT, z = pred),bins = 30,
                  binwidth = c(0.05,0.05)) +

  scale_fill_gradientn(colours=c("white","blue","green","yellow","red"),
                       values=rescale(c(0,50,100,150,200)),
                       guide="colorbar", name = "PM10 Conc")+ 
  geom_map(data= myKorea1, map= myKorea1, aes(x=long,y=lat,map_id=id,group=group),
           fill=NA, colour="black") +

  coord_cartesian(xlim= c(126.6, 127.2), ylim= c(37.2 ,37.7)) +
  labs(title= "PM10 Concentration in Seoul Area at South Korea",
       x="", y= "")+
  theme(legend.position = "bottom")+
  guides(fill = guide_colourbar(barwidth = 27, barheight = NULL,
                               title.position = "bottom", title.hjust = 0.5)) 

这是一个可行的解决方案:

library(scales)
library(ggplot2)
library(raster) # needed for the `getData` function
library(dplyr)  # needed for the `mutate` funtion

myKrige_new <- read.csv("~/Downloads/myKrige_new.csv")[-1]

range(myKrige_new$LON) 
range(myKrige_new$LAT)

# Original skorea data transformed the same was as myKrige_new
skorea1 <- getData("GADM", country= "KOR", level=1)
skorea1 <- fortify(skorea1)  
myKorea1 <- data.frame(skorea1)

# the range of  pred goes above 200 (max = 257)
summary(myKrige_new$pred) 

ggplot() + 
  theme_minimal() +
  stat_summary_2d(data = mutate(myKrige_new,
                                pred = ifelse(pred > 200, 200, pred)),
                  aes(x = LON, y = LAT, z = pred),
                  bins = 30,
                  binwidth = c(0.05,0.05)) +
  scale_fill_gradientn(colours=c("white","blue","green","yellow","red"),
                       values=rescale(c(0,50,100,150,200)),
                       name = expression(paste(PM[10], group("[",paste(mu,g/m^3), "]"))),
                       limits = c(0,200),
                       breaks = seq(0,200, 20),
                       guide = guide_colorbar(nbin = 20,
                                              barwidth = 27,
                                              title.position = "bottom",
                                              title.hjust = 0.5, 
                                              raster = FALSE,
                                              ticks = FALSE)) + 
  geom_map(data= myKorea1,
           map= myKorea1,
           aes(x=long,y=lat,map_id=id,group=group),
           fill=NA,
           colour="black") +
  coord_equal(xlim= c(126.6, 127.2),
              ylim= c(37.2 ,37.7)) +
  scale_y_continuous(expand = c(0,0)) +
  scale_x_continuous(expand = c(0,0)) +
  labs(title = "PM10 Concentration in Seoul Area at South Korea",
       x = "",
       y = "") +
  theme(legend.position = "bottom")

我将 limits = c(0,200)breaks = seq(0, 200, 20) 添加到 scale_fill_gradientn 以及 nbin = 20guide_colorbar,最后的更改是可选的,因为默认值 nbin 是 20,但在你的情况下你实际上需要 20。此外,添加 limits 意味着范围外的值绘制在 grey50 中,所以我不得不将 pred 值转换为 200 以上到 200 来避免这种情况;红色的解释现在是200+。

还有一件事,guide_colorbar 中的选项 raster 将颜色栏从光栅对象更改为一组矩形,实现您正在寻找的外观。

最后,我将坐标系从笛卡尔坐标系更改为相等坐标系,因为您正在绘制地图。

结果如下,希望对您有所帮助:

更新:根据 OP

的要求向 scale_y_continuousscale_x_continuous 添加了一个 expand 参数