在ggplot2中的地图上围绕点绘制具有特定半径的圆 [英] Plot circle with a certain radius around point on a map in ggplot2

查看:210
本文介绍了在ggplot2中的地图上围绕点绘制具有特定半径的圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(ggplot2)
library(ggmap) )
data = data.frame(
ID = as.numeric(c(1:8)),
longitude = as.numeric(c(-63.27462,-63.26499,-63.25658, (c(17.6328,17.64614,17.64755,17.64632,17.64888,17.63113,17.61252,17.62463)),


island = get_map(location = c(lon = -63.247593,lat = 17.631598),zoom = 13,maptype =satellite)
islandMap = ggmap(island,extent =面板,legend =bottomright)
RL = geom_point(aes(x = longitude,y = latitude),data = data,color =#ff0000)
islandMap + RL + scale_x_continuous = c(-63.280,-63.21),expand = c(0,0))+ scale_y_continuous(limits = c(17.605,17.66),expand = c(0,0))

现在我想围绕8个绘制位置中的每一个绘制一个圆圈。圆圈必须有450米的半径。

这就是我的意思,但是接着使用ggplot: https://gis.stackexchange.com/questions/119736/ggmap-create-circle-symbol-where-radius-represents-distance-miles- or-km



我该如何做到这一点?

解决方案

如果你只在地球的一小部分地区工作,这是一个近似值。每个纬度代表40075/360公里。每个经度代表(40075/360)* cos(纬度)千米。有了这个,我们可以计算大约一个数据框,包括圆圈上的所有点,知道圆心和半径。

  library(ggplot2)
library(ggmap)
data = data.frame(
ID = as.numeric(c(1:8)),
longitude = as.numeric(c(-63.27462,-63.26499,-63.25658,-63.2519,-63.2311,-63.2175,-63.23623,-63.25958) ),
latitude = as.numeric(c(17.6328,17.64614,17.64755,17.64632,17.64888,17.63113,17.61252,17.62463))


###### ################################################## #########################
#从中心数据框中创建圆圈数据框
make_circles< - 函数(中心,半径,n点= 100){
#个中心:ID为中心的数据帧b $ b#半径:以公里为单位测量的半径

meanLat < - mean纬度)
#每经度的长度随着地面变化而变化,因此需要校正
radiusLon< - 半径/ 111 / cos(meanLat / 57.3)
radiusLat< - 半径/ 111
circleDF< - data.frame(ID = rep(center $ ID,each = nPoints))
angl e - seq(0,2 * pi,length.out = nPoints)

circleDF $ lon < - unlist(lapply(center $ longitude,function(x)x + radiusLon * cos角度)))
circleDF $ lat< - unlist(lapply(centers $ latitude,function(x)x + radiusLat * sin(angle)))
return(circleDF)
}

#这里是所有圈子的数据框
myCircles< - make_circles(data,0.45)
################ ################################################## ################


island = get_map(location = c(lon = -63.247593,lat = 17.631598),zoom = 13, maptype =satellite)
islandMap = ggmap(island,extent =panel,legend =bottomright)
RL = geom_point(aes(x = longitude,y = latitude),data = data ,color =#ff0000)
islandMap + RL +
scale_x_continuous(limits = c(-63.280,-63.21),expand = c(0,0))+
scale_y_continuous = c(17.605,17.66),expand = c(0,0))+
###########添加圆圈
geom_polygon(data = myCircles,aes(l on,lat,group = ID),color =red,alpha = 0)


I have a map with the 8 points plotted on it:

library(ggplot2)
library(ggmap)
data = data.frame(
    ID = as.numeric(c(1:8)),
    longitude = as.numeric(c(-63.27462, -63.26499, -63.25658, -63.2519, -63.2311, -63.2175, -63.23623, -63.25958)),
    latitude = as.numeric(c(17.6328, 17.64614, 17.64755, 17.64632, 17.64888, 17.63113, 17.61252, 17.62463))
)

island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 13, maptype = "satellite")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, color = "#ff0000")
islandMap + RL + scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) + scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0))

Now I want to plot a circle around each of the 8 plotted locations. The circle has to have a radius of 450 meters.

This is what I mean, but then using ggplot: https://gis.stackexchange.com/questions/119736/ggmap-create-circle-symbol-where-radius-represents-distance-miles-or-km

How can I achieve this?

解决方案

If you only work on a small area of the earth, here is a approximation. Each degree of the latitude represents 40075 / 360 kilometers. Each degrees of longitude represents (40075 / 360) * cos(latitude) kilomemters. With this, we can calculate approximately a data frame including all points on circles, knowing the circle centers and radius.

library(ggplot2)
library(ggmap)
data = data.frame(
    ID = as.numeric(c(1:8)),
    longitude = as.numeric(c(-63.27462, -63.26499, -63.25658, -63.2519, -63.2311, -63.2175, -63.23623, -63.25958)),
    latitude = as.numeric(c(17.6328, 17.64614, 17.64755, 17.64632, 17.64888, 17.63113, 17.61252, 17.62463))
)

#################################################################################
# create circles data frame from the centers data frame
make_circles <- function(centers, radius, nPoints = 100){
    # centers: the data frame of centers with ID
    # radius: radius measured in kilometer
    #
    meanLat <- mean(centers$latitude)
    # length per longitude changes with lattitude, so need correction
    radiusLon <- radius /111 / cos(meanLat/57.3) 
    radiusLat <- radius / 111
    circleDF <- data.frame(ID = rep(centers$ID, each = nPoints))
    angle <- seq(0,2*pi,length.out = nPoints)

    circleDF$lon <- unlist(lapply(centers$longitude, function(x) x + radiusLon * cos(angle)))
    circleDF$lat <- unlist(lapply(centers$latitude, function(x) x + radiusLat * sin(angle)))
    return(circleDF)
}

# here is the data frame for all circles
myCircles <- make_circles(data, 0.45)
##################################################################################


island = get_map(location = c(lon = -63.247593, lat = 17.631598), zoom = 13, maptype = "satellite")
islandMap = ggmap(island, extent = "panel", legend = "bottomright")
RL = geom_point(aes(x = longitude, y = latitude), data = data, color = "#ff0000")
islandMap + RL + 
    scale_x_continuous(limits = c(-63.280, -63.21), expand = c(0, 0)) + 
    scale_y_continuous(limits = c(17.605, 17.66), expand = c(0, 0)) +
    ########### add circles
    geom_polygon(data = myCircles, aes(lon, lat, group = ID), color = "red", alpha = 0)

这篇关于在ggplot2中的地图上围绕点绘制具有特定半径的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆