格式化ggplot中的纬度和经度轴标签 [英] Format latitude and longitude axis labels in ggplot

查看:235
本文介绍了格式化ggplot中的纬度和经度轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

  library(ggmap)
ggmap(get_map())

我想将轴标签自动标记为NS / WE:在上述情况下,例如,而不是lon -95.4它应该显示95.4°E。



我试图弄乱比例包和使用 scale_x_continuous scale_y_continuous 标签和中断选项,但我没有设法使它工作。 p>

拥有 scale_y_latitude scale_x_longitude 会很棒。



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



<$ p $ (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(Long itude,break = ewbrks,labels = ewlbls,expand = c(0,0),...))
}
scale_y_latitude < - 函数(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

解决方案

没有 scale_x_longitude scale_y_latitude 之类的东西。与此同时,这是一个解决方法,您可以事先指定标签:

 #加载所需的库
库( ggplot2)
library(ggmap)

#获取地图
m< - get_map(location = c(lon = 0,lat = 0),zoom = 5)

#创建断点和标签向量
ewbrks< - seq(-10,10,5)
nsbrks< - seq(-10,10,5)
ewlbls< - 未列出(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))))

#创建地图
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 作为上标(这将避开需要一个特殊符号):

pre $ scale_x_longitude < - 函数(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< - 函数(ymin = -90,ymax = 90,step = 0.5,...){
ybreaks <-seq(ymin,ymax,step)
ylabels< - unlist(lapply(ybreaks ,函数(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 )在地图上绘制数据。


I have a ggplot map, for example:

library(ggmap)
ggmap(get_map())

I'd like the axis labels to be automatically labeled as N-S / W-E: in the above case, for example, instead of lon -95.4 it should show 95.4°E.

I have tried to mess with the scales package and using scale_x_continuous and scale_y_continuous labels and breaks options, but I have not managed to make it work.

It would be awesome to have a scale_y_latitude and scale_x_longitude.

EDIT: Thanks to @Jaap 's answer I got to the following:

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), ...))
}

Which works pretty well. But for some reason my R doesn't seem to like the degree symbol in front of the cardinal point... It is displayed as a simple dot, e.g. longitude -24 becomes 24..W

解决方案

Unfortunately, there is no such thing as scale_x_longitude or scale_y_latitude yet. In the meantime here is a workaround in which you specify the labels beforehand:

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

which gives:


To get the degrees in the functions, you can raise the o as superscript (which will circumvent the need for a special symbol):

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

which gives the following map:

I used geom_blank just to illustrate the desired effect. You can off course use other geom's (e.g. geom_point) to plot your data on the map.

这篇关于格式化ggplot中的纬度和经度轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆