gpt4 book ai didi

r - 使用 ggmap/ggplot2 在 map 上按组绘制等高线

转载 作者:行者123 更新时间:2023-12-02 04:26:14 27 4
gpt4 key购买 nike

所以我认为我有一个非常简单的问题,但我无法在任何地方找到答案。

我有很多包含龙虾捕获量的数据。一切看起来都很像这样。

                        Trip.ID Latitude Longitude            DateTime               ML6      TotalNephropsLandings
16409 OTB_CRU_32-69_0_0DK102831 57.931 9.277 2012-10-04 19:02:00 OTB_CRU_32-69_0_0 0.2188619
16410 OTB_CRU_32-69_0_0DK102831 57.959 9.375 2012-10-04 21:02:00 OTB_CRU_32-69_0_0 0.2188619
16411 OTB_CRU_32-69_0_0DK102831 58.201 10.232 2012-10-04 02:00:00 OTB_CRU_32-69_0_0 0.2188619
16412 OTB_CRU_32-69_0_0DK102831 58.208 10.260 2012-10-04 03:00:00 OTB_CRU_32-69_0_0 0.2188619
16413 OTB_CRU_32-69_0_0DK102831 58.169 10.078 2012-10-03 23:00:00 OTB_CRU_32-69_0_0 0.2188619
16414 OTB_CRU_32-69_0_0DK102831 57.919 9.227 2012-10-04 18:00:00 OTB_CRU_32-69_0_0 0.2188619

我想做的只是根据“ML6”列制作一张包含区域周围轮廓的 map ,这是用于钓鱼的不同工具。

我尝试使用 geom_densis2d,它看起来像这样: 然而我真的不想展示密度,只想展示它们存在的地方。因此,基本上一条线围绕一组坐标,这些坐标来自 ML6 中的同一级别。有人能帮我解决这个问题吗?

如果有替代方案将它们也填充为多边形,那就太好了。但也许可以使用“fill=”简单地完成。

如果有人知道如何在没有 R 的情况下做到这一点,也欢迎您提供帮助,但那样我可能需要更深入的信息。

很抱歉没有生成更多的数据框...

当然,我应该生成我的情节代码,所以这里基本上是:

#Get map
map <- get_map(location=c(left= 0, bottom=45, right=15 ,top=70), maptype = 'satellite')
ggmap(map, extent="normal") +
geom_density2d(data = df, aes(x=Longitude, y=Latitude, group=ML6, colour=ML6))

最佳答案

可能有更好的方法来完成这项工作。但是,这是我为您提供的方法。我希望这种方法适用于 ggmap以及。鉴于我有时间,这是我对你最好的。由于您的样本数据太小,我决定使用我自己的部分数据。您要做的就是查看ggplot_build(objectname)$data[1] 。 (看起来,当您使用 ggmap 时,数据将在 ggplot_build(object name)$data[4] 中。)例如,创建一个像这样的对象。

foo <- ggmap(map, extent="normal") + 
geom_density2d(data = df, aes(x=Longitude, y=Latitude, group=ML6, colour=ML6))

然后,输入 ggplot_build(foo)$data[1] 。您应该看到一个数据框 ggplot正在使用。将有一个名为 level 的列。具有最小级别值的数据子集。我正在使用filter来自dplyr 。例如,

foo2 <- ggplot_build(foo)$data[1]
foo3 <- filter(foo2, level == 0.02)

foo3 现在有数据点,允许您在 map 上画线。该数据包含关卡最外圈的数据点。你会看到类似这样的东西。

#     fill level        x         y piece group PANEL
#1 #3287BD 0.02 168.3333 -45.22235 1 1-001 1
#2 #3287BD 0.02 168.3149 -45.09596 1 1-001 1
#3 #3287BD 0.02 168.3197 -44.95455 1 1-001 1

然后,您将执行如下操作。就我而言,我没有谷歌地图。我有新西兰的 map 数据。所以我用第一个 geom_path 画这个国家。第二个geom_path就是你需要的。确保将 lon 和 lat 更改为 x 和 y,如下所示。这样我认为您就拥有了想要的圆圈。

# Map data from gadm.org
NZmap <- readOGR(dsn=".",layer="NZL_adm2")
map.df <- fortify(NZmap)

ggplot(NULL)+
geom_path(data = map.df,aes(x = long, y = lat, group=group), colour="grey50") +
geom_path(data = foo3, aes(x = x, y = y,group = group), colour="red")

enter image description here

更新

这是另一种方法。在这里我使用了我的答案 this post 。您基本上可以确定数据点来绘制圆(多边形)。我在帖子里有一些链接。请看一看。您可以了解循环中发生了什么。抱歉很短。但是,我认为这种方法可以让你画出你想要的所有圆圈。请记住,结果可能不是像轮廓那样光滑的圆圈。

library(ggmap)
library(sp)
library(rgdal)
library(data.table)
library(plyr)
library(dplyr)

### This is also from my old answer.
### Set a range
lat <- c(44.49,44.5)
lon <- c(11.33,11.36)

### Get a map
map <- get_map(location = c(lon = mean(lon), lat = mean(lat)), zoom = 14,
maptype = "satellite", source = "google")

### Create pseudo data.
foo <- data.frame(x = runif(50, 11.345, 11.357),
y= runif(50, 44.4924, 44.4978),
group = "one",
stringsAsFactors = FALSE)

foo2 <- data.frame(x = runif(50, 11.331, 11.338),
y= runif(50, 44.4924, 44.4978),
group = "two",
stringsAsFactors = FALSE)

new <- rbind(foo,foo2)

### Loop through and create data points to draw a polygon for each group.
cats <- list()

for(i in unique(new$group)){

foo <- new %>%
filter(group == i) %>%
select(x, y)

ch <- chull(foo)
coords <- foo[c(ch, ch[1]), ]

sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID=1)))

bob <- fortify(sp_poly)

bob$area <- i

cats[[i]] <- bob
}

cathy <- as.data.frame(rbindlist(cats))

ggmap(map) +
geom_path(data = cathy, aes(x = long, y = lat, group = area), colour="red") +
scale_x_continuous(limits = c(11.33, 11.36), expand = c(0, 0)) +
scale_y_continuous(limits = c(44.49, 44.5), expand = c(0, 0))

enter image description here

关于r - 使用 ggmap/ggplot2 在 map 上按组绘制等高线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26842278/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com