0

全部,

请参阅下面的 R 脚本代码。我只是想用商店(storeLocations)和客户(CustomerLocations)列表填充英国地图。

STRTRADECODE 是 StoreLocations 表中的列名,其中包含特定商店的名称。

我无法输出标签。请帮忙。

提前致谢。

library(RgoogleMaps)
library(maps)
library(ggmap) 
library(ggplot)

UKMap <- qmap("United Kingdom", zoom = 6.0)

storeOverlay <- geom_point(aes(x = longitude, y = latitude),
                             data = StoreLocations,  colour = "red") 

storeOverlay <- storeOverlay + geom_text(data= StoreLocations, aes(label=STRTRADECODE))

CustomerOverlay <- geom_point(aes(x = longitude, y = latitude),
                           data = CustomerLocations,  colour = "green")

UKMap + CustomerOverlay + storeOverlay
4

1 回答 1

0

正如我之前在评论中所说,您必须将 lon 和 lat 添加到 geom_text,所以 ggplot 知道将文本放在哪里。

这是一个工作示例(我包括了 nudge_x,因此文本/标签并不直接指向这一点)

library(RgoogleMaps)
library(maps)
library(ggmap) 
library(ggplot)

STRTRADECODE <- c("London","Sheffield","Glasgow")

StoreLocations <- as.data.frame(STRTRADECODE,stringsAsFactors=F)

StoreLocations %>%
  mutate_geocode(STRTRADECODE) %>%
  rename(longitude = lon,latitude=lat) -> StoreLocations

CustomerLocations <- StoreLocations
CustomerLocations$longitude <- CustomerLocations$longitude - 1

UKMap <- qmap("United Kingdom", zoom = 6.0)

UKMap +
  geom_point(mapping=aes(x = longitude, 
                         y = latitude),
            data = StoreLocations,  
            colour = "red"
            ) +
  geom_text( 
            mapping=aes(x = longitude, 
                        y = latitude,
                        label = STRTRADECODE
                        ),
            data= StoreLocations,
            nudge_x = 0.8
            ) +
 geom_point(aes(x = longitude, 
                y = latitude
                ),
            data = CustomerLocations, 
            colour = "green"
            )
于 2016-08-25T10:06:11.167 回答