2017-08-15 5 views
1

質問/問題は元のquestionと非常によく似ています。小数点以下の桁数をggplotの軸ラベルで分単位の秒数に変更してください。「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 

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

Second map

2番目のマップでは、2つの座標だけが描画され、間違った形式です。 私は次のように書かれたこれらのコルディネイトが必要です:

55ºW、54ºW54 '、54ºW48'、54ºW42 '; 22ºS24 '、22ºS18'、22ºS12 '、22ºS06'

誰でも手伝ってもらえますか?

アップデート(16/08/2017) @Rafael Cunha(更新ありがとうございます)のアップデートコードです。 分記号を追加する方法がまだありません。しかし、以前よりもうまくいっています。私は度分秒に小数度を変換するためにGEOmapパッケージから機能dmsを使用

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) 

答えて

0

@ThiagoシルバのTeles、

ビル@Rafaelクーニャは(おかげで、私はなります提供されているコードのオフこれを使っても)、式の関数は(私にとってはとにかく)プロット軸に度、分、秒のラベルを提供するように機能します。

ggmap軸プロットのためにDDをDMSに変換する関数。 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) 

ため

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

例のマップは、私はそれが正しく表示されて所望の間隔で持つように(機能コードとコール以内)に「ステップ」をいじくり回す必要がありました。これは、より大きいスケールで秒または分を省略するように改善することができます。私は非常に小さなスケールで10進数秒を提供するのが好きです。プログラマー/コーダーの多くではありませんが、これはうまくいくようです。

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

+0

ありがとう@ C.Aldridge!それは働いている;) –

0

。私のコードで欠けているのは、軸ラベルに分を貼り付ける方法だけです。

ライブラリ(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)