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

如何根据R/ggplot2中的特定值范围创建图例(连续色条)?,r,ggplot2,legend,R,Ggplot2,Legend,我有一个名为myKrige\u new的数据框,其中包含一些纬度长插值。您可以从下载。我使用R中的ggplot2软件包在国家地图的特定区域绘制了这些值,得到了这个图 但我希望我的故事的传奇(色条)像下面的传奇一样 在我这里的数据集中,数据的范围(pred)是72到257。但是我希望我的图例会显示0到200的值,因为将原因与其他绘图进行比较,尽管在72下没有值 所以,我想使用20种不同的颜色,比如上面的图例,这意味着图例的最后一个框将包含大于200的颜色。我使用了scale\u fill\u

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

但我希望我的故事的传奇(色条)像下面的传奇一样

在我这里的数据集中,数据的范围(pred)是72到257。但是我希望我的图例会显示0到200的值,因为将原因与其他绘图进行比较,尽管在72下没有值

所以,我想使用20种不同的颜色,比如上面的图例,这意味着图例的最后一个框将包含大于200的颜色。我使用了
scale\u fill\u 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)) 
库(比例)
图书馆(GG2)

myKrige_new以下是一个有效的解决方案:

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")
库(比例)
图书馆(GG2)
“getData”函数所需的库(光栅)
库(dplyr)#用于“突变”功能

myKrige_new以下是一个有效的解决方案:

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")
库(比例)
图书馆(GG2)
“getData”函数所需的库(光栅)
库(dplyr)#用于“突变”功能

myKrige\u新设置
比例填充\u梯度n(…,guide='legend',…)
而不是
颜色条
,尽管结果因几何体而异。抱歉,我无法计算出来!实际上,我想为不同的值范围显示不同的颜色,图例将显示颜色0到200的范围。设置
scale\u fill\u gradientn(…,guide='legend',…)
而不是
colorbar
,尽管结果因几何体而异。抱歉,我无法计算出来!实际上,我想为不同的值范围显示不同的颜色,图例将显示颜色0到200的范围。哇!非常感谢donlelek!如果你不介意的话,还有一件事我正在努力解决。你们可以看到,地图的某些部分延伸到了我的绘图的右侧和底部。有什么方法可以删除/剪切这个扩展部分吗?嗨@Orpheus,刚刚编辑了解决这个问题的答案,你必须在你想要裁剪的轴上添加
expand=c(0,0)
。亲爱的@donleke,我如何在这个图上添加一些文本?我想在我的绘图上写上“PM10[微克/立方米]:2012/03/28/23 KST”。(PM10和m3中的10和3为下标)。我试着这样做:
annotate(“text”,label=expression(粘贴(PM[10],group(“[”,粘贴(mu,g/m^3),“])):2012/03/28/23~KST),x=311000,y=4173500,size=6,color=“white”,fontface=c(“bold”)
但它给了我
错误:无效的参数类型(表达式)
Hi@Orpheus,这段代码有不止一个问题,首先
geom_text
不接受表达式,它们必须是字符串,你必须添加
parse=TRUE
,其次你的x和y在不同的尺度上,我猜你切换到了UTM,最后,括号放错了位置,下面的代码应该可以解决你的问题。第1页,共22页,共2页
注释(“text”,label=“PM[10]~group('[',mu*g/m^3'])*”:“*2012/03/28/23*”~'*KST”,x=127,y=37.5,parse=TRUE,size=6,color=“black”,fontface=“bold”)
wow!非常感谢donlelek!如果你不介意的话,还有一件事我正在努力解决。你们可以看到,地图的某些部分延伸到了我的绘图的右侧和底部。有什么方法可以删除/剪切这个扩展部分吗?嗨@Orpheus,刚刚编辑了解决这个问题的答案,你必须在你想要裁剪的轴上添加
expand=c(0,0)
。亲爱的@donleke,我如何在这个图上添加一些文本?我想在我的绘图上写上“PM10[微克/立方米]:2012/03/28/23 KST”。(PM10和m3中的10和3为下标)。我试着这样做:
annotate(“text”,label=expression(粘贴(PM[10],group(“[”,粘贴(mu,g/m^3),“])):2012/03/28/23~KST),x=311000,y=4173500,size=6,color=“white”,fontface=c(“bold”)
但它给了我
错误:无效的参数类型(表达式)
Hi@Orpheus,这段代码有不止一个问题,首先
geom_text
不接受表达式,它们必须是字符串,你必须添加
parse=TRUE
,其次你的x和y在不同的尺度上,我猜你切换到了UTM,最后,括号放错了位置,下面的代码应该可以解决你的问题。第1页,共22页,共2页
注释(“text”,label=“PM[10]~group('[',mu*g/m^3'])*”:“*2012/03/28/23*”~'*KST”,x=127,y=37.5,parse=TRUE,size=6,color=“black”,fontface=“bold”)