2017-10-20 142 views
1

我有一个围绕中心点的纬度数据框。拉通代表两组。这些组以1或0表示。我可以围绕中心点绘制不同的形状 和颜色,但是我希望颜色更清晰。我还想分别围绕300米和600米两点划两圈。我曾尝试与围绕数据中心点绘制两个圆圈

Plot circle with a certain radius around point on a map in ggplot2

工作,但有没有运气

一个小样本看起来像这样

lat <- c(42.99052, 42.99085, 42.99046, 42.99081, 42.99197, 42.99122, 42.99154,42.99161, 42.99102, 42.99014, 42.98966, 42.99091, 42.99092, 42.99114 ,42.99000) 
lon <-c(78.69961, -78.69871, -78.69878, -78.69868, -78.69825, -78.69929, -78.69784, -78.69960, -78.69904, -78.69918, -78.69998, -78.69746, -78.70145, -78.70020, -78.70010) 
response <- c(0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1) 
data <- data.frame(lat, lon, response) 

我的中心点是

(42.990707, -78.698899) 

我得到我的没有圆圈和不同颜色的地图,像这样

library(ggplot2) 
library(ggmap) 
library(viridis) 

#getting map 

mapgilbert <- get_map(location = c(lon = -78.698899 , lat=42.990707), zoom = 17, 
        maptype = "terrain", scale = 2) 

ggmap(mapgilbert) + 
    geom_point(data = data, aes(x = lon, y = lat, color = response , alpha = 1, fill = response), 
      size = 2, shape = response) + 
    guides(fill = FALSE, alpha = FALSE, size = FALSE) + 
    scale_color_viridis(begin = 0, end = .7) 

任何帮助,将不胜感激。

+0

得到了解决,我建议您按照从您链接到的问题这样的回答:https://stackoverflow.com/a/34187878/ 7446435您可能需要了解一些关于投影的内容,但它基本上是最简单的方法,可以准确获得您所需的内容。 – Jul

+0

@ Mikel Arnaiz,你为什么要画圈子? ;-) –

回答

0

我通过修改答案

Plot circle with a certain radius around point on a map in ggplot2

center_df<-data.frame(ID=c(1),lon=c(-78.698899),lat=c(42.990707)) 

make_circles <- function(centers, radius, nPoints = 500){ 
    # centers: the data frame of centers with ID 
    # radius: radius measured in kilometer 
    # 
    meanLat <- mean(centers$lat) 
    # 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$lon, function(x) x + radiusLon * cos(angle))) 
    circleDF$lat <- unlist(lapply(centers$lat, function(x) x + radiusLat * sin(angle))) 
    return(circleDF) 
} 



myCircles <- make_circles(center_df, 0.2) 
myCircles2 <- make_circles(center_df, 0.1) 
mapgilbert2<- get_map(location = c(lon = -78.70000 , lat=42.991107), zoom = 16, 
        maptype = "terrain", scale = 2) 

ggmap(mapgilbert2)+ 
geom_point(data = data, aes(x = lon, y = lat, fill=as.factor(exposure) , alpha = 1), size = 2, pch=21)+ 
guides(fill=FALSE, alpha=FALSE, size=guide_legend)+ 
scale_color_viridis(begin = 0, end = .7)+ 
geom_point(data=myCircles,aes(x=lon,y=lat), color='red')+ 
geom_point(data=myCircles2,aes(x=lon,y=lat), color='blue')+ 
scale_fill_manual(values=c("purple", "green"))