2016-01-02 36 views
14

我试图绘制我在阿拉伯/波斯湾研究的一些生物种类的生态分布。这里是我试过的代码示例:ggplot_stat_density2d生态分布图

底色层

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_density2d显示数百种物种的分布(记录在例如列SP1 .... SPn)而不仅仅是显示经度和纬度。

另外,是否有可能将我的热图限制在水体中? 我感谢所有帮助和建议,我可以得到这个讨好image generated with the code above

+1

这将是有益的,得到一些数据sample.data3的。 'dput(head(sample.data3,20))'应该足够在'ggplot'中播放 – Vedda

+3

需水量很有趣。 –

+0

@Amstell,这里是一个压缩文件夹的链接,其中包含一个Rstudi项目,其中包含我正在使用的所有示例数据和多边形。 [链接](https://www.dropbox.com/s/5zssgq4kqlykbf0/Persian%20Gulf.rar?dl=0)谢谢 – Hammao

回答

2

我的方法你的问题是一个务实的一个:简单地说海湾国家的层热图分布。这相应地产生了热图。但请注意,热图仍然按照未裁剪的方式进行计算。这意味着密度计算是而不是仅限于水体,但它只是在视觉上裁剪。

为了可重复性,以下代码假定您已经解压@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

+0

这正是我想要做的......然而,我不想只是拉特和长期,我想绘制每个变量...(我会结束几个地块)。 lat&long仅用于空间定位变量。我将通过添加一个循环语句来修改此答案,以便您答案的“fill = ..level ..”将适应变化的SP1 --- SPn。我希望这使得最初的问题更清楚。 – Hammao

+0

好的谢谢澄清。我已经更新了答案。要分别为每个物种绘制纬度/长度,您还可以尝试使用刻面。由于种类太多,所以会有很多方面,然而,阅读情节可能会变得困难。因此,按照您的建议循环播种物种,并保存单独的地块可能是最好的办法。 – Felix

+0

感谢您的帮助... – Hammao