r - ggplot_stat_密度2d 生态分布图

标签 r ggplot2 density-plot

我正在尝试绘制我正在阿拉伯/波斯湾研究的某些生物物种的生态分布。这是我尝试过的代码示例:

背景层

library(ggplot2)
library(ggmap)

nc <- get_map("Persian Gulf", zoom = 6, maptype = 'terrain', language = "English")
ncmap <- ggmap(nc,  extent = "device")

其他层

  ncmap+
    stat_density2d(data=sample.data3, aes(x=long, y=lat, fill=..level.., alpha=..level..),geom="polygon")+
    geom_point(data=sample.data3, aes(x=long, y=lat))+
    geom_point(aes(x =50.626444, y = 26.044472), color="red", size = 4)+
    scale_fill_gradient(low = "green", high = "red") + scale_alpha(range = c(0.00, 0.25), guide = FALSE)

但是,我想使用 stat_密度2d 来显示水体上数百个物种的分布(记录在列中,例如 SP1....SPn),而不仅仅是显示纬度和经度。

此外,是否可以将我的热图限制为仅显示水体? 如果我能得到任何帮助和建议,我将不胜感激image generated with the code above

最佳答案

我对你的问题的处理方法是一种务实的方法:只需将海湾国家图层放在热图分布上。这会相应地裁剪热图。但请注意,热图仍然按照未裁剪的方式进行计算。这意味着密度计算不仅仅仅限于水体,但它只是视觉上被裁剪。

为了重现性,以下代码假设您已解压 @Hammao 提供的 .rar 文件,并在生成的 Persian Gulf 文件夹中执行代码。

# get sample data
sample.data <- read.csv("sample.data3.csv")

现在,我们需要获取海湾国家的国家形状。我用rworldmap为此的包。

# loading country shapes
library(rworldmap) 

# download map of the world
worldmap <- getMap(resolution = "high") # note that for 'resolution="high"' 
                                        # you also need the "rworldxtra" pkg

# extract Persian Gulf countries...
gulf_simpl <- worldmap[worldmap$SOVEREIGNT == "Oman" | 
                         worldmap$SOVEREIGNT == "Qatar"  |
                         worldmap$SOVEREIGNT == "United Arab Emirates" |
                         worldmap$SOVEREIGNT == "Bahrain" |
                         worldmap$SOVEREIGNT == "Saudi Arabia" |
                         worldmap$SOVEREIGNT == "Kuwait" |
                         worldmap$SOVEREIGNT == "Iraq" |
                         worldmap$SOVEREIGNT == "Iran", ]

# ... and fortify the data for plotting in ggplot2
gulf_simpl_fort <- fortify(gulf_simpl)

# Now read data for the Persian Gulf, which we need to get the distances for
# the extension of the map
PG <- readOGR(dsn = ".", "iho")
PG <- readShapePoly("iho.shp")

PG <- fortify(PG)

现在,只需按照正确的顺序绘制图层即可。

# generate plot
ggplot(sample.data) + 

  # first we plot the density...
  stat_density_2d(aes(x = long, y = lat, 
                      fill = ..level..),
                  geom="polygon", 
                  alpha = 0.5) +

  # ... then we plot the points
  geom_point(aes(x = long, y = lat)) +

  # gradient options
  scale_fill_gradient(low = "green", high = "red") + 
  scale_alpha(range = c(0.00, 0.25), guide = FALSE) +

  # and now put the shapes of the gulf states on top
  geom_polygon(data = gulf_simpl_fort, 
               aes(x = long, 
                   y = lat, group = group), 
               color = "black", fill = "white", 
               inherit.aes = F) +

  # now, limit the displayed map only to the gulf 
  coord_equal(xlim = c(min(PG_fort$long), max(PG_fort$long)), 
              ylim = c(min(PG_fort$lat), max(PG_fort$lat))) +
  theme_bw()

enter image description here

关于r - ggplot_stat_密度2d 生态分布图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34569152/

相关文章:

r - 如何更改/指定超出渐变条限制的填充颜色?

R 子集函数,包括 '[' 不适用于大型数据帧/矩阵的中间范围

r - R中数据框列的拆分和组合

r - MTTF 的置信区间 - R 中的威 bool 生存曲线

r - lm() 或 gls() 的正系数

python - 如何使用时间序列数据在 ggplot 中绘制面积图?

r - 游泳者生存图 ggplot_Events 按持续时间进行颜色编码

r - 重叠堆积密度图

gnuplot - 在 pm3d map 中画一条线

r - 为什么 geom_密度 绘制的数据与预期图像不同?