gpt4 book ai didi

r - 无法在 ggplot2 上绘制比例尺或指北针

转载 作者:行者123 更新时间:2023-12-03 13:34:32 27 4
gpt4 key购买 nike

帮助!我有一个森林中树木的 GPS x/y 位置数据集,并将此代码转换为 ggplot 中的漂亮 map :

#ggmap!
library(ggmap)
library(mapproj)
map <- get_map(location = 'Madagascar', zoom = 10)
geocode("kianjavato")
#lon lat
#47.86682 -21.38024
k <- "kianjavato"
myMap <- get_map(location=k, source="stamen", maptype="toner", crop=FALSE, zoom=16)
ggmap(myMap)
m <- ggmap(myMap) + geom_point(aes(y = Lat, x = Lon, colour = Foraged, shape=Plot), data = GPS)
n <- m + scale_colour_manual(values=c("blue", "red3")) +
scale_shape_discrete(solid=F, legend=F) +
scale_y_continuous(limits=c(-21.376,-21.3715)) +
scale_x_continuous(limits=c(47.865,47.869))
plot(n)

但是,无论我尝试什么类型的代码,我都无法在 map 上绘制指向北方的箭头或比例尺!我尝试了很多解决方案,其中一些看起来像这样:

尝试添加比例尺
map.scale <- ggmap(new) + (ggmap, extent = "normal", maprange = FALSE) %+% sites.data +
geom_point(aes(x = lon, y = lat, colour = colour)) +
geom_text(aes(x = lon, y = lat, label = label), hjust = 0, vjust = 0.5, size = 8/ptspermm) +
geom_segment(data = sbar, aes(x = lon.start, xend = lon.end,
y = lat.start, yend = lat.end)) +
geom_text(data = sbar, aes(x = (lon.start + lon.end)/2,
y = lat.start + 0.025*(bb$ur.lat - bb$ll.lat),
label = paste(format(distance, digits = 4, nsmall = 2),'km')),
hjust = 0.5, vjust = 0, size = 8/ptspermm) +
coord_map(projection="mercator", xlim=c(bb$ll.lon, bb$ur.lon),
ylim=c(bb$ll.lat, bb$ur.lat))

#library(SDMTools)
#Scalebar(x=47.868,y=-21.375,distance=100,unit='m') #show values in meters
#Error in map.scale(x = 50, y = -22) : argument "len" is missing, with no default

map.scale(x=47.868, y=-21.375, ratio=FALSE, relwidth=0.2)
#Error in map.scale(x = 47.868, y = -21.375, ratio = FALSE, relwidth = 0.2) :
unused argument(s) (ratio = FALSE, relwidth = 0.2)
map.scale(x=47.868, y=-21.375)
#Error in map.scale(x = 47.868, y = -21.375) :
argument "len" is missing, with no default

我需要指北针和比例尺,但都不会绘制!为什么?

最佳答案

ggsn包在这里很有用。没有 GPS 数据,我无法执行您的所有脚本,但是鉴于指定的 x 和 y 限制,这应该在合理的位置添加北符号和比例尺。

#First, add a scale bar.
n <- n + scalebar(location="bottomright",y.min=-21.3755, y.max=-21.3715,
x.min=47.865, x.max=47.869, dist=.1, dd2km= TRUE, model='WGS84',
st.dist=.04)

#Now add a north arrow and plot the map.
north2(n, x=.3, y=.8, symbol=9)

north symbol and scale bar example

关于r - 无法在 ggplot2 上绘制比例尺或指北针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26259590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com