【问题标题】:Format latitude and longitude axis labels in ggplot在ggplot中格式化纬度和经度轴标签
【发布时间】:2016-01-22 23:52:50
【问题描述】:

我有一个ggplot图,例如:

library(ggmap)
ggmap(get_map())

我希望轴标签自动标记为 N-S / W-E:例如,在上述情况下,应该显示 95.4°E 而不是 lon -95.4。

我试图弄乱scales 包并使用scale_x_continuousscale_y_continuous 标签和中断选项,但我没有设法使它工作。

拥有scale_y_latitudescale_x_longitude 会很棒。

编辑: 感谢@Jaap 的回答,我得到了以下信息:

scale_x_longitude <- function(xmin=-180, xmax=180, step=1, ...) {
    ewbrks <- seq(xmin,xmax,step)
    ewlbls <- unlist(lapply(ewbrks, function(x) ifelse(x < 0, paste(x, "W"), ifelse(x > 0, paste(x, "E"),x))))
    return(scale_x_continuous("Longitude", breaks = ewbrks, labels = ewlbls, expand = c(0, 0), ...))
}
scale_y_latitude <- function(ymin=-90, ymax=90, step=0.5, ...) {
    nsbrks <- seq(ymin,ymax,step)
    nslbls <- unlist(lapply(nsbrks, function(x) ifelse(x < 0, paste(x, "S"), ifelse(x > 0, paste(x, "N"),x))))
    return(scale_y_continuous("Latitude", breaks = nsbrks, labels = nslbls, expand = c(0, 0), ...))
}

效果很好。但由于某种原因,我的 R 似乎不喜欢基点前面的度数符号......它显示为一个简单的点,例如经度 -24 变为 24..W

【问题讨论】:

    标签: r ggplot2 latitude-longitude axis-labels ggmap


    【解决方案1】:

    不幸的是,还没有scale_x_longitudescale_y_latitude 这样的东西。同时,这里有一个解决方法,您可以在其中预先指定标签:

    # load the needed libraries
    library(ggplot2)
    library(ggmap)
    
    # get the map
    m <- get_map(location=c(lon=0,lat=0),zoom=5)
    
    # create the breaks- and label vectors
    ewbrks <- seq(-10,10,5)
    nsbrks <- seq(-10,10,5)
    ewlbls <- unlist(lapply(ewbrks, function(x) ifelse(x < 0, paste(x, "°E"), ifelse(x > 0, paste(x, "°W"),x))))
    nslbls <- unlist(lapply(nsbrks, function(x) ifelse(x < 0, paste(x, "°S"), ifelse(x > 0, paste(x, "°N"),x))))
    
    # create the map
    ggmap(m) +
      geom_blank() +
      scale_x_continuous(breaks = ewbrks, labels = ewlbls, expand = c(0, 0)) +
      scale_y_continuous(breaks = nsbrks, labels = nslbls, expand = c(0, 0)) +
      theme(axis.text = element_text(size=12))
    

    给出:


    要获得函数中的度数,您可以将o 提高为上标(这将避免对特殊符号的需要):

    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), ...))
    }    
    
    ggmap(m) +
      geom_blank() +
      scale_x_longitude(xmin=-10, xmax=10, step=5) +
      scale_y_latitude(ymin=-10, ymax=10, step=5) +
      theme(axis.text = element_text(size=12))
    

    它给出了以下地图:

    我使用geom_blank 只是为了说明所需的效果。您当然可以使用其他几何图形(例如geom_point)在地图上绘制数据。

    【讨论】:

    • 答案非常好,谢谢,但我没有将其标记为已接受,因为我正在寻找一种自动化方法,例如在执行library(scales); p + scale_y_continuous(labels = percent) 时(请参阅docs.ggplot2.org/current/scale_continuous.html)...顺便说一句,我的 R 似乎不喜欢“°E”中的度数符号......由于某种原因它没有正确呈现。想法?
    • @AF7 我想我找到了度数符号的解决方案。查看更新。
    • 是否可以将轴也放在顶部和右侧?在函数的返回中添加sec.axis = dup_axis() 只会添加纬度/经度标签,而不是刻度和数字:imgur.com/a/BcQew
    • 顺便说一句,将x 解析为-x 的西坐标和南坐标会更好。
    • 关于 Alfredo 的评论,人们也可能使用模数来删除负数(SQRT(x^2),除非有人知道直接函数);我必须使用它来检查它是否正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2012-02-16
    • 2013-05-22
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2021-10-11
    相关资源
    最近更新 更多