2016-08-25 83 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" 
      )