0

我正在尝试绘制一个国家的热图,其中一些点是事件发生的概率。我到目前为止所做的是下一步:

library(raster)
library(ggplot2)
Uruguay  <- getData("GADM",country="Uruguay",level=0)
ggplot(Uruguay,aes(x=long,y=lat,group=group)) + 
  ggplot2::lims(x = c(-60, -50), y = c(-35, -30))+
  geom_polygon(aes(x = long, y = lat, group = group, fill=id),color="grey30")+
  coord_map(xlim=c(-1,1)+bbox(Uruguay)["x",],ylim=c(-1,1)+bbox(Uruguay)["y",])+
  scale_fill_discrete(guide="none")+
  theme_bw()+theme(panel.grid=element_blank())

国家边界

我生成热图的数据是

prob <- c(10,20,90,40)
lat <- c(-30.52,-32.04,-33.16,-34.28)
long <- c(-57.40,-55.45,-56.35,-56.40)
data <- data.frame(prob, lat, long)

我认为使用ggplot2::stat_density2dandggplot2::scale_fill_gradientn是要走的路,但我不知道如何实现它。我想制作这样的热图

在此处输入图像描述

欢迎任何帮助。提前致谢。

4

2 回答 2

0

要绘制示例数据,您可以使用 plot

library(raster)
Uruguay  <- getData("GADM",country="Uruguay",level=0)
plot(Uruguay, col="orange")

至于你要制作的地图,有很多选择。但这是一个基本的例子

prob <- c(10,20,90,40)
lat <- c(-30.52,-32.04,-33.16,-34.28)
long <- c(-57.40,-55.45,-56.35,-56.40)
data <- data.frame(prob, lat, long)

r <- raster(Uruguay, res=.5)
x <- rasterize(cbind(long, lat), r, prob)
plot(x)
lines(Uruguay)
于 2021-03-23T19:52:39.487 回答
0

终于可以得到我想要的了。Henrik 在这篇文章中的回答非常有帮助,我与您分享代码

library(raster)
library(reshape2)
library(ggplot2)
Uruguay  <- getData("GADM",country="Uruguay",level=1)

#invented data
    prob <- c(5, 90,10,15,99,40,90,25,70,90)
    lat <- c(-31,-31.2,-31.3,-34,-32.5,-32.6,-33.7,-34.9,-34.2,-32.5)
    long <- c(-58.3,-55.1,-57.3,-58.4,-56.5,-54,-57.7,-55.8,-54.1,-53.5)
    prueba <- data.frame(prob, lat, long)
    


library(akima)
fld <- with(prueba, interp(x = long, y = lat, z = prob))

class(Uruguay)
uru <- fortify(Uruguay)

library(reshape2)

# prepare data in long format
df <- melt(fld$z, na.rm = TRUE)
names(df) <- c("x", "y", "prob")
df$long <- fld$x[df$x]
df$lat <- fld$y[df$y]


ggplot() + 
  geom_polygon(data = uru, aes(x = long, y = lat, group = group),
               colour = "black", size = 0.5, fill = "white") +
  geom_tile(data = df, aes(x = long, y = lat, z = prob, fill = prob), alpha = 0.8) +
  ggtitle("Frost probability") +
  xlab("Longitude") +
  ylab("Latitude") +
  scale_fill_continuous(name = "Probability (%)",
                        low = "red", high = "blue") +
  theme_bw() +`enter code here`
  theme(plot.title = element_text(size = 25, face = "bold"),
        legend.title = element_text(size = 15),
        axis.text = element_text(size = 15),
        axis.title.x = element_text(size = 20, vjust = -0.5),
        axis.title.y = element_text(size = 20, vjust = 0.2),
        legend.text = element_text(size = 10)) +
  coord_map()
于 2021-03-27T09:08:19.820 回答