ggplot2中的定位符等价物(对于地图) [英] locator equivalent in ggplot2 (for maps)

查看:136
本文介绍了ggplot2中的定位符等价物(对于地图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这个问题是特定于映射的,但我希望能够在标准笛卡尔坐标系中绘制时使用它。



我喜欢基础图形,但也喜欢ggplot2。我最常用的基本函数之一是用于精细调整图形的locator(n),但这会在ggplot2中产生一个错误。

  library (ggplot2)
county_df< - map_data('county')#按县划分县的数量
ny< - 子集(county_df,region ==new york)#subset仅用于NYS
ny $ county < - ny $ subregion

ggplot(ny,aes(long,lat,group = group))+ geom_polygon(color ='black',fill = NA)
locator(1)

现在 grid.locator()正如Dason在talkstats.com上指出的那样(这里)可以返回一些东西。我只是不知道如何使用这些东西来获得地图坐标。

 > grid.locator()
$ x
[1] 286native

$ y
[1] 133native

单位似乎没有帮助,因为它们不是地图坐标。也许我需要某种转换。

预先感谢您。

(根据迪文的回应)

Dwin有正确的想法,但转换因子有点偏离。帮助与此将不胜感激。在下面的例子中,我在坐标(x = -73& y = 40.855)处有一个红点的地图。我将Dwin的响应投入到一个返回坐标的函数中。我希望结果是我放入的坐标,但它们不是。 想法?

  require(maps);库(GGPLOT2);要求(网格)

county_df< - map_data('county')#按县划分的州县
ny< - 子集(县_df,地区==纽约州)#subset (ny,aes(long,lat))+
geom_polygon(仅适用于NYS
ny $ county < - ny $ subregion




$ b aes(group = group),color ='black',fill = NA)+
coord_map()+ geom_point(aes(-73,40.855,color =red))
NY

gglocator< - function(object){
require(maps);通过< - sapply(z,function(x)as.numeric(substring(x,1,nchar(x)))来获得(网格)
z< - grid.locator(npc)$ b $ )
locatedX < - min(object $ data $ long)+ y [1] * diff(range(object $ data $ long))
locatedy < - min(object $ data $ lat) + y [2] * diff(range(object $ data $ lat))
return(c(locatedX,locatedy))
}

#点击红点
gglocator(NY)#I我预计结果是x = -73& y = 40.855

编辑2 :(从Baptise的回答开始)



我们在那里

  NY < -  ggplot(ny, aes(long,lat))+ 
geom_polygon(aes(group = group),color ='black',fill = NA)+
coord_map()+ geom_point(aes(-73,40.855,color = expand_y_continuous(expand = c(0,0))


NY
x< - grid.ls()[[1]] [grep(panel-,grid.ls()[[1]])]#找到面板
seekViewport(x)
y < - grid.locator(npc)
y< - as.numeric(substring(y,1,nchar(y)-3))

locatedX< - min NY(NY $ data $ long)+ y [1] * diff(range(NY $ data $ long))
locatedy < - min(NY $ data $ lat)+ y [2] * diff(range(NY $ data $ lat))
locatedX;位于

更新 gglocator 函数 ggmap package a>现在包含此功能。 c $ c> scale_x_continuous(expand = c(0,0))+ scale_y_continuous(expand = c(0,0))绘图, seekViewport(panel- 3-4)之前> grid.locator()


Note: This question is specific for mapping but I'd like to be able to use it when I plot in a standard Cartesian coordinate system.

I love base graphics but also like ggplot2 for many things. One of my most used base functions for fine tuning a graph is locator(n) but this produces an error in ggplot2.

library(ggplot2) 
county_df <- map_data('county')  #mappings of counties by state
ny <- subset(county_df, region=="new york")   #subset just for NYS
ny$county <- ny$subregion

ggplot(ny, aes(long, lat, group=group)) +  geom_polygon(colour='black', fill=NA)
locator(1)

Now grid.locator() as pointed out to me by Dason on talkstats.com (HERE) can return something. I just don't know how to use that something to get a map coordinate.

> grid.locator()
$x
[1] 286native

$y
[1] 133native

Units didn't seem to help as they are not map coordinates. Maybe I need some sort of conversion.

Thank you in advance.

EDIT: (based on DWin's response)

Dwin's got the right idea but the conversion factor is a little bit off. Help with that would be appreciated. In the example below I have a map with a red dot on it at the coordinates (x = -73 & y = 40.855). I threw Dwin's response into a function to return the coordinates. I would expect the results to be the coordinates I put in but they're not.

Ideas?

require(maps); library(ggplot2); require(grid)

county_df <- map_data('county')  #mappings of counties by state
ny <- subset(county_df, region=="new york")   #subset just for NYS
ny$county <- ny$subregion


NY <- ggplot(ny, aes(long, lat)) +  
          geom_polygon(aes(group=group), colour='black', fill=NA) +
          coord_map() + geom_point(aes(-73, 40.855, colour="red"))
NY  

gglocator <- function(object){
    require(maps); require(grid)
    z <- grid.locator("npc")
    y <- sapply(z, function(x) as.numeric(substring(x, 1, nchar(x))))
    locatedX <- min(object$data$long) + y[1]*diff(range(object$data$long))
    locatedy <- min(object$data$lat)  + y[2]*diff(range(object$data$lat))
    return(c(locatedX, locatedy))
}

#click on the red dot
gglocator(NY)  #I expect the results to be x = -73 & y = 40.855

EDIT 2: (Going off of Baptise's answer)

We're there

NY <- ggplot(ny, aes(long, lat)) +  
          geom_polygon(aes(group=group), colour='black', fill=NA) +
          coord_map() + geom_point(aes(-73, 40.855, colour="red")) +
          scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))


NY 
x <- grid.ls()[[1]][grep("panel-", grid.ls()[[1]])] #locate the panel
seekViewport(x)
y <-  grid.locator("npc")
y <- as.numeric(substring(y, 1, nchar(y)-3))

locatedX <- min(NY$data$long) + y[1]*diff(range(NY$data$long))
locatedy <- min(NY$data$lat) + y[2]*diff(range(NY$data$lat))
locatedX; locatedy 

UPDATE: The gglocator function of the ggmap package now contains this functionality.

解决方案

I get the correct result if I add scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0)) to the plot, and seekViewport("panel-3-4") before grid.locator()

这篇关于ggplot2中的定位符等价物(对于地图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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