使用ggmap和geom_point时,geom_text无法正常工作

Jd *_*aba 8 plot r ggplot2 ggmap

我正在使用ggmap和gg_point函数来显示河口的测量数据.

我使用的代码如下:

    library(ggmap)
al1 <- get_map(location = c(lon = -87.525, lat = 30.35), zoom = 12, maptype = 'terrain')

lon<- c(-87.604474,-87.55)
lat<- c(30.362563,30.35)
label <- c("A","B")
df<-data.frame(lon,lat,label)

p <- ggmap(al1)+geom_point(data=df,aes(x=lon,y=lat,shape=label,label=label),size=3)
p <- p + xlab("Longitude")+ylab("Latitude")
p <- p +geom_text(aes(label=label, size=3,vjust=0))
p <- p + labs(title="Monitoring stations ")
p

ggsave("plot.pdf")
Run Code Online (Sandbox Code Playgroud)

在这里,当我使用geom_text时,我得到以下错误,"美学必须是长度为1,或与dataProblems:label相同的长度".

我想将标签贴在图中的点旁边.我想放置两个点和标签,并留有一些间距,以便更容易阅读.

我查看了这篇文章" ggplot legend issue w/geom_point和geom_text "并尝试修改我的代码,如上所示,但我不知道为什么我遇到这个问题.

还有另一篇文章如何说服ggplot2 geom_text标记时间序列图中的指定日期?谈到类似的问题.我得到的结果不同,因为我也在使用ggmap吗?

请帮我解决这个问题.非常感谢.

Jdbaba

jor*_*ran 10

您的问题是您没有geom_text正确指定美学:

geom_text(data = df, aes(x = lon, y = lat, label = label), 
          size = 3, vjust = 0, hjust = -0.5)
Run Code Online (Sandbox Code Playgroud)

您没有告诉geom_text使用数据框中的变量df.如果你不这样做,所有的美学都是从主要的呼唤中继承下来的.最后,当将美学设置为单个值时,您不会在内部aes(),而是在外部执行此操作.

我对hjust设置进行了调整以使标签可见.