0

All,

Please see below code to a R script. I am simply trying to populate a map of UK with list of stores (storeLocations) and customers (CustomerLocations).

STRTRADECODE is the column name within StoreLocations table which contains name of a particular store.

I am unable to output the labels. Please help.

Thanks in advance.

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
dllhell
  • 1,987
  • 3
  • 34
  • 51
  • Try to add longitude and latitude to the geom_text element like this: `geom_text(data= StoreLocations, aes(x = longitude, y = latitude,label=STRTRADECODE))` – PhiSeu Aug 25 '16 at 09:15
  • @PhiSeu Thank you but still unable to populate the labels. – Kajan Sittampalam Aug 25 '16 at 10:03
  • Try my Answer. Include the `data=` Attribute in all geom's. In a normal ggplot you define the aes in the `ggplot(aes())` function, and the geom's inherit them. Here I define it in every step. - Is there a error message of some kind? Or a warning? – PhiSeu Aug 25 '16 at 10:12
  • @PhiSeu using your example below, i am getting the following error: Error in eval(expr, envir, enclos) : object 'longitude' not found – Kajan Sittampalam Aug 25 '16 at 10:37
  • Might it be that the column in your dataframe is not called longitude? Could you please post the structure of your data with `str(StoreLocation)` – PhiSeu Aug 25 '16 at 12:39

1 Answers1

0

As I said in my commentary before, you have to add the lon and lat to geom_text, so ggplot knows, where to put the text.

Here is a working example (I included nudge_x, so the Text/Label is not directly on the point)

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"
            )
PhiSeu
  • 301
  • 2
  • 9