【问题标题】:Revisiting the "Format latitude and longitude axis labels in ggplot"重温“在 ggplot 中格式化纬度和经度轴标签”
【发布时间】:2017-10-26 01:52:26
【问题描述】:

问题/问题与原始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

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

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

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)

【问题讨论】:

    标签: r ggplot2 ggmap


    【解决方案1】:

    @蒂亚戈席尔瓦泰勒斯,

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

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

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

    【讨论】:

      【解决方案2】:

      我使用 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)
      

      【讨论】:

        猜你喜欢
        • 2016-01-22
        • 1970-01-01
        • 2011-12-17
        • 2012-02-16
        • 2013-05-22
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        • 2021-10-11
        相关资源
        最近更新 更多