2016-08-25 80 views
0

全部,R - 标签geom_point()

请参阅下面的代码到一个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 
+0

尝试添加经度和纬度向geom_text元件是这样的:'geom_text(数据= StoreLocations,aes(x = longitude,y = latitude,label = STRTRADECODE))' – PhiSeu

+0

@PhiSeu谢谢你,但仍然无法填充标签。 –

+0

试试我的答案。在所有geom中包含'data ='属性。在一个正常的ggplot中,你定义了'ggplot(aes())'函数中的aes,并且geom继承了它们。我在这里每一步都定义它。 - 是否有某种错误信息?还是警告? – PhiSeu

回答

0

正如我在评论中说之前,你必须添加LON和LAT到geom_text,所以ggplot知道,在那里把文本。

这里是一个工作示例(I包括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" 
      )