简体   繁体   中英

Plot contours by groups on map with ggmap/ggplot2

So I think I have a pretty simple question, but I can't find the answer anywhere.

I have a lot of data containing catches of lobsters. It all pretty much looks like this.

                        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

What I would like to do is simply make a map with contours around areas based on the "ML6" column, which are different tools used for fishing.

I tried using geom_density2d, which looks like this: However I really don't want to show density, only where they are present. So basically one line around a group of coordinates that are from the same level in ML6. Could anyone help me with this?

It would also be nice to have the alternative to fill these in as polygons as well. But perhaps that could simple be accomplished using "fill=".

If anyone knows how to do this without R, you are also welcome to help, but then I would possibly need more in depth information.

Sorry for not producing more of my data frame...

Of course I should have produced the code I had for the plot, so here it is basically:

#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))

There are probably better way of doing this work. But, here is my approach for you. I hope this approach works with ggmap as well. Given time I have, this is my best for you. Since your sample data is way too small, I decided to use a part of my own data. What you want to do is to look into ggplot_build(objectname)$data[1] . (It seems that, when you use ggmap , data would be in ggplot_build(object name)$data[4] .) For example, create an object like this.

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

Then, type ggplot_build(foo)$data[1] . You should see a data frame which ggplot is using. There will be a column called level . Subset data with the minimum level value. I am using filter from dplyr . For example,

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

foo3 now has data point which allows you to draw lines on your map. This data has the data points for the most outer circles of the level. You would see something like this.

#     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

Then, you would do something like the following. In my case, I do not have googlemap. I have a map data of New Zealand. So I am drawing the country with the first geom_path . The second geom_path is the one you need. Make sure you change lon and lat to x and y like below.In this way I think you have the circles you want.

# 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")

在此处输入图片说明

UPDATE

Here is another approach. Here I used my answer from this post . You basically identify data points to draw a circle (polygon). I have some links in the post. Please have a look. You can learn what is happening in the loop. Sorry for being short. But, I think this approach allows you to draw all circles you want. Remind that the outcome may not be nice smooth circles like contours.

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))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM