【发布时间】: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)
【问题讨论】: