在boarders()中使用xlim和ylim的问题以及在R中使用geom_text()插入文本

时间:2016-11-09 07:10:30

标签: r ggplot2

以下是我的R代码:

library("ggplot2")

xlon <- c(11.9, 156.6) # deg. E
ylat <- c(78.9, 71.3)  # deg. N

#Using GGPLOT, plot the Base World Map
mp <- NULL
mapWorld <- borders("world", colour="black", ylim=c(40, 90), xlim=c(0, 200), 
                    col="white",exact=TRUE, fill="gray100") # create a layer of borders
mp <- ggplot() + mapWorld

#Now Layer the cities on top
mp <- mp+ geom_point(aes(x=xlon, y=ylat) ,color="blue", size=3)
mp

我的代码的目的是绘制一个带有网格框的世界地图,该网格框界定了40-90度的确切区域。 N和0-200 deg E.当我运行上面的代码时,我发现y限制范围从20到90度。 N而x的极限似乎在0度E之前开始。任何人都可以建议我如何获得指定的x和y方向的精确边界?

此外,我想在地图上包含两个点的位置标签。这两点是:

巴罗:71.3N,156.6 E. Ny-Ålesund:78.9 N,11.9 E

除了积分之外,有没有人可以帮我解释如何包含“Barrow”和“Ny-Ålesund”?我试过geom_text(),但没能成功。

2 个答案:

答案 0 :(得分:2)

要添加文字,您可以尝试以下方法:

df <- data.frame(lat=c(71.3, 78.9), lon=c(156.6, 11.9), label=c('Barrow', 'Ny-Ålesund'))
mp + geom_text(data = df, aes(x = lon, y = lat, label = label), 
               size = 5, vjust = 1, hjust = 1)

enter image description here

尝试此操作来限制x和y轴:

df <- data.frame(lat=c(71.3, 78.9), lon=c(156.6, 11.9), label=c('Barrow', 'Ny-Ålesund'))
mp <- ggplot(df, aes(x=lon, y=ylat)) + mapWorld + 
  xlim(0, 200) + ylim(40, 90)
mp + geom_point(aes(x=xlon, y=ylat) ,color="blue", size=3) +
  geom_text( aes(x = lon, y = lat, label = label), 
               size = 5, vjust = 1, hjust = 0.75, col='red')

enter image description here

尝试使用圆形投影(您可以更改方向以获得最佳效果):

mp + geom_point(aes(x=xlon, y=ylat) ,color="blue", size=5) +
  geom_text( aes(x = lon, y = lat, label = label), 
               size = 5, vjust = 1, hjust = 0.75, col='red') + coord_map("ortho", orientation=c(25, 25, 0)) # for ortho maps

enter image description here

答案 1 :(得分:0)

以下是脚本:

library("ggplot2")
df <- data.frame(lat=c(71.3, 78.9), lon=c(156.6, 11.9), label=c('Barrow', 'Ny-Ålesund'))
mp <- NULL
mapWorld <- borders("world", colour="black",col="white", fill="gray100") # create a layer of borders
mp <- ggplot(df, aes(x=lon, y=lat)) + mapWorld + xlim(0, 360) + ylim(40, 90)+ 
  geom_point(aes(x=lon, y=lat) ,color="blue", size=2.5) +
  geom_text( aes(x = lon, y = lat, label = label), 
             size=3,vjust=1,hjust=0.75,col='red') + 
  coord_map("ortho", orientation=c(90,90,0))
mp

This is the resulting figure from the above code

但我需要以下类型的数字: Required figures

如何更改上面的代码以获得必要的投影或方向?