ggplot2中的定位器等价物(用于地图)

Tyl*_*ker 26 r ggplot2 ggmap

注意:这个问题是专门用于映射的,但是当我在标准的笛卡尔坐标系中绘图时,我希望能够使用它.

我喜欢基本图形,但也喜欢ggplot2.用于微调图形的我最常用的基本函数之一是locator(n),但这会在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)
Run Code Online (Sandbox Code Playgroud)

现在,grid.locator()作为talkstats.com向我指出的达诚(这里),可以返回一些东西.我只是不知道如何使用那些东西来获得地图坐标.

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

$y
[1] 133native
Run Code Online (Sandbox Code Playgroud)

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

先感谢您.

编辑:(基于DWin的回复)

Dwin有正确的想法,但转换因素有点偏.对此的帮助将不胜感激.在下面的示例中,我在坐标处有一个红点(x = -73&y = 40.855).我把Dwin的响应扔进了一个函数来返回坐标.我希望结果是我输入的坐标,但它们不是.

想法?

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
Run Code Online (Sandbox Code Playgroud)

编辑2 :(关闭巴蒂斯的回答)

在那里

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 
Run Code Online (Sandbox Code Playgroud)

更新: ggmap包gglocator功能现在包含此功能.

42-*_*42- 9

需要使用有意义的单位系统并将信息保存在ggplot对象中,以便您可以从"npc"单位转换为地图单位:

require(maps)
require(grid)
NY <- ggplot(ny, aes(long, lat, group=group)) +  geom_polygon(colour='black', fill=NA)
 grid.locator("npc")
# clicked in middle of NY State:

#$x
#[1] 0.493649231346082npc
#
#$y
#[1] 0.556430446194226npc
 range(NY$data$long)
#[1] -79.76718 -71.87756
 range(NY$data$lat)
#[1] 40.48520 45.01157
 locatedX <- min(NY$data$long) + 0.493649231346082*diff(range(NY$data$long))
 locatedX
#[1] -75.87247
locatedY <- min(NY$data$lat) +  0.556430446194226*diff(range(NY$data$lat))
locatedY
#[1] 43.00381
Run Code Online (Sandbox Code Playgroud)


bap*_*ste 7

我得到正确的结果,如果我加scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))的情节,和seekViewport("panel-3-4")grid.locator()


Tyl*_*ker 5

这是使用DWin和Baptise给我包装成函数的所有东西的最终结果.我还询问了ggplot帮助列表,并将在此处报告和其他信息.

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

ny <- map_data('county', 'new york')

NY1 <- ggplot(ny, aes(long, lat)) +  
          geom_polygon(aes(group=group), colour='black', fill=NA) +
          coord_map() + geom_point(aes(c(-78, -73), c(41, 40.855), 
          colour=c("blue", "red"))) + opts(legend.position = "none") 

NY <- NY1 + scale_x_continuous(expand=c(0,0)) + 
          scale_y_continuous(expand=c(0,0))
          #the scale x and y have to be added to the plot

NY 

ggmap.loc <- function(object){
    x <- grid.ls()[[1]][grep("panel-", grid.ls()[[1]])] #locate the panel
    seekViewport(x)
    y <-  as.numeric(grid.locator("npc"))
    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))
}

ggmap.loc(NY)
Run Code Online (Sandbox Code Playgroud)


Tyl*_*ker 5

我写信给ggplot帮助列表,收到David Kahle的一个非常有用的回复,他恰好对同样的问题感兴趣.他的功能很棒:

1)您不必添加比例y并将x缩放到图中

2)它可以一次找到多个点并将它们作为数据帧返回

3)它适用于任何类型的ggplot,而不仅仅是地图

gglocator <- function(n = 1, object = last_plot(), 
    message = FALSE, xexpand = c(.05,0), yexpand = c(.05, 0)){ 

  #compliments of David Kahle
  if(n > 1){
    df <- NULL
    for(k in 1:n){
      df <- rbind(df, gglocator(object = object, message = message, 
        xexpand = xexpand, yexpand = yexpand))
    }
    return(df)
  }

  x <- grid.ls(print = message)[[1]]
  x <- x[ grep("panel-", grid.ls(print=message)[[1]]) ] #locate the panel
  seekViewport(x)
  loc <-  as.numeric(grid.locator("npc"))

  xrng <- with(object, range(data[,deparse(mapping$x)]))
  yrng <- with(object, range(data[,deparse(mapping$y)]))    

  xrng <- expand_range(range = xrng, mul = xexpand[1], add = xexpand[2])
  yrng <- expand_range(range = yrng, mul = yexpand[1], add = yexpand[2])    

  point <- data.frame(xrng[1] + loc[1]*diff(xrng), yrng[1] + loc[2]*diff(yrng))
  names(point) <- with(object, c(deparse(mapping$x), deparse(mapping$y)))
  point
}

#Example 1
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(c(-78, -73), c(41, 40.855), 
          colour=c("blue", "red"))) + opts(legend.position = "none") 


NY 
gglocator(2)

#Example 2
df <- data.frame(xvar = 2:10, yvar = 2:10)
ggplot(df, aes(xvar, yvar)) + geom_point() + geom_point(aes(x = 3, y = 6))
gglocator()
Run Code Online (Sandbox Code Playgroud)

更新: ggmap包gglocator功能现在包含此功能.

  • 这是向前迈出的一步,但仍然不完全令人满意:1)如果轴不是线性的,它可能会返回不正确的结果; 2)当页面上有多个面板/图时,它可能以某种方式失败. (2认同)