重温 "Format latitude and longitude axis labels in ggplot"

Revisiting the "Format latitude and longitude axis labels in ggplot"

question/problem 与原来的非常相似 :将 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

此外,我正在尝试@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)

第二张地图只画了两个坐标,而且格式不对。 我需要这些 corrdinates 写如下:

55ºW, 54ºW 54',54ºW 48', 54ºW 42'; 22ºS 24'、22ºS 18'、22ºS 12'、22ºS 06'

谁能帮帮我?

更新 (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)

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

图书馆(ggplot2) 图书馆(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")

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)

@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)

我不得不修改 "step"(在函数代码和调用中)以使其正确显示并按所需的时间间隔显示。这仍然可以改进以在更大的范围内省略秒或分钟。我喜欢它以非常小的比例提供小数秒。不多 programmer/coder,但这似乎有效。

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