重新审视“在ggplot中格式化纬度和经度轴标签”

时间:2017-08-15 17:52:00

标签: r ggplot2 ggmap

问题/问题与原始question非常相似:在ggplot轴标签中将十进制度数更改为度数分钟秒。

我正在执行以下步骤:

library(ggplot2)
library(ggmap)

#get my map
city<- get_map(location = c(lon= -54.847, lat= -22.25),
                maptype = "satellite",zoom = 11,color="bw")

map<-ggmap(city,extent="normal")+
  xlab("Longitude")+ ylab("Latitude")
map

First map

另外,我正在尝试@Jaap所写的内容:

scale_x_longitude <- function(xmin=-180, xmax=180, step=1, ...) {
  xbreaks <- seq(xmin,xmax,step)
  xlabels <- unlist(lapply(xbreaks, function(x) ifelse(x < 0, parse(text=paste0(x,"^o", "*W")), ifelse(x > 0, parse(text=paste0(x,"^o", "*E")),x))))
  return(scale_x_continuous("Longitude", breaks = xbreaks, labels = xlabels, expand = c(0, 0), ...))
}
scale_y_latitude <- function(ymin=-90, ymax=90, step=0.5, ...) {
  ybreaks <- seq(ymin,ymax,step)
  ylabels <- unlist(lapply(ybreaks, function(x) ifelse(x < 0, parse(text=paste0(x,"^o", "*S")), ifelse(x > 0, parse(text=paste0(x,"^o", "*N")),x))))
  return(scale_y_continuous("Latitude", breaks = ybreaks, labels = ylabels, expand = c(0, 0), ...))
} 

所以:

map+
  scale_x_longitude(-55.0,-54.7,4)+
  scale_y_latitude(-22.4,-22.1,4)

Second map

在第二张地图中,只绘制了两个坐标,格式错误。 我需要这些corrdinates写如下:

55ºW,54ºW54',54ºW48',54ºW42'; 22º24',22ºS18',22ºS12',22ºS06'

任何人都可以帮助我吗?

更新(16/08/2017) 这是由@Rafael Cunha提供的更新代码(非常感谢!) 仍然缺少添加分钟符号的方法。但是,它比以前工作得更好。

scale_x_longitude <- function(xmin=-180, xmax=180, step=1, ...) {
  xbreaks <- seq(xmin,xmax,step)
  xlabels <- unlist(
    lapply(xbreaks, function(x){
      ifelse(x < 0, parse(text=paste0(paste0(abs(dms(x)$d),"^{o}*"),
                                      paste0(abs(dms(x)$m)), "*W")), 
             ifelse(x > 0, parse(text=paste0(paste0(abs(dms(x)$d),"^{o}*"),
                                             paste0(abs(dms(x)$m)),"*E")),
                    abs(dms(x))))}))
  return(scale_x_continuous("Longitude", breaks = xbreaks, labels = xlabels, expand = c(0, 0), ...))
}

scale_y_latitude <- function(ymin=-90, ymax=90, step=0.5, ...) {
  ybreaks <- seq(ymin,ymax,step)
  ylabels <- unlist(
    lapply(ybreaks, function(x){
      ifelse(x < 0, parse(text=paste0(paste0(abs(dms(x)$d),"^{o}*"),
                                      paste0(abs(dms(x)$m)),"*S"), 
             ifelse(x > 0, parse(text=paste0(paste0(abs(dms(x)$d),"^{o}*"),
                                             paste0(abs(dms(x)$m)),"*N")),
                    abs(dms(x))))}))
  return(scale_y_continuous("Latitude", breaks = ybreaks, labels = ylabels, expand = c(0, 0), ...))
}  

map+
  scale_x_longitude(-55.0,-54.7,.1)+
  scale_y_latitude(-22.4,-22.1,.1)

2 个答案:

答案 0 :(得分:0)

我使用dms包中的函数GEOmap将十进制度转换为度数分钟秒。我的代码中唯一缺少的是将分钟粘贴到轴标签中的方法。

库(GGPLOT2)     库(ggmap)     库(GEOMAP)

#get my map
city<- get_map(location = c(lon= -54.847, lat= -22.25),
               maptype = "satellite",zoom = 11,color="bw")

map<-ggmap(city,extent="normal")+
  xlab("Longitude")+ ylab("Latitude")

scale_x_longitude <- function(xmin=-180, xmax=180, step=1, ...) {
  xbreaks <- seq(xmin,xmax,step)
  xlabels <- unlist(lapply(xbreaks, function(x) ifelse(x < 0, parse(text=paste0(abs(dms(x)$d),"^o", "*W")), ifelse(x > 0, parse(text=paste0(abs(dms(x)$d),"^o", "*E")),abs(dms(x))))))
  return(scale_x_continuous("Longitude", breaks = xbreaks, labels = xlabels, expand = c(0, 0), ...))
}
scale_y_latitude <- function(ymin=-90, ymax=90, step=0.5, ...) {
  ybreaks <- seq(ymin,ymax,step)
  ylabels <- unlist(lapply(ybreaks, function(x) ifelse(x < 0, parse(text=paste0(abs(dms(x)$d),"^o", "*S")), ifelse(x > 0, parse(text=paste0(abs(dms(x)$d),"^o", "*N")),abs(dms(x))))))
  return(scale_y_continuous("Latitude", breaks = ybreaks, labels = ylabels, expand = c(0, 0), ...))
} 

map+
  scale_x_longitude(-55.0,-54.7,.1)+
  scale_y_latitude(-22.4,-22.1,.1)

答案 1 :(得分:0)

@Thiago Silva Teles,

建立@Rafael Cunha提供的代码(谢谢,我也将使用它),表达式函数(无论如何为我工作)在绘图轴上提供度,分和秒标签。

将DD转换为DMS以进行ggmap轴绘图的功能。

scale_x_longitude <- function(xmin=-180, xmax=180, step=0.002, ...) {
  xbreaks <- seq(xmin,xmax,step)
  xlabels <- unlist(
    lapply(xbreaks, function(x){
      ifelse(x < 0, parse(text=paste0(paste0(abs(dms(x)$d), expression("*{degree}*")),
                                      paste0(abs(dms(x)$m), expression("*{minute}*")),
                                      paste0(abs(dms(x)$s)), expression("*{second}*W"))), 
             ifelse(x > 0, parse(text=paste0(paste0(abs(dms(x)$d), expression("*{degree}*")),
                                             paste0(abs(dms(x)$m), expression("*{minute}*")),
                                             paste0(abs(dms(x)$s)), expression("*{second}*E"))),
                    abs(dms(x))))}))
  return(scale_x_continuous("Longitude", breaks = xbreaks, labels = xlabels, expand = c(0, 0), ...))
}

scale_y_latitude <- function(ymin=-90, ymax=90, step=0.002, ...) {
  ybreaks <- seq(ymin,ymax,step)
  ylabels <- unlist(
    lapply(ybreaks, function(x){
      ifelse(x < 0, parse(text=paste0(paste0(abs(dms(x)$d), expression("*{degree}*")),
                                      paste0(abs(dms(x)$m), expression("*{minute}*")),
                                      paste0(abs(dms(x)$s)), expression("*{second}*S"))), 
             ifelse(x > 0, parse(text=paste0(paste0(abs(dms(x)$d), expression("*{degree}*")),
                                             paste0(abs(dms(x)$m), expression("*{minute}*")),
                                             paste0(abs(dms(x)$s)), expression("*{second}*N"))),
                    abs(dms(x))))}))
  return(scale_y_continuous("Latitude", breaks = ybreaks, labels = ylabels, expand = c(0, 0), ...))
}  

Stackexchange的示例映射

library(ggplot2)
library(ggmap)
map <- get_map(location = "Alabama",
               zoom = 8,
               maptype = "toner", source = "stamen",
               color = "bw")
sam_map <- ggmap(map) +
  theme_minimal() + theme(legend.position = "none")

sam_map +
  scale_x_longitude(-89, -85, 0.75) +
  scale_y_latitude(30, 34, 0.75)

我不得不修补&#34;步骤&#34; (在功能代码和调用中)使其以所需的间隔正确显示。这仍然可以改进,以省略更大规模的秒或分钟。我喜欢它在非常小的尺度上提供小数秒。程序员/编码员并不多,但这确实有效。

Map of LA (Lower Alabama) with DMS (proper formatting)