Overlapping labels ggmap
Asked Answered
M

2

5

I have the Google map and a list of coordinates with a text label. When I preview this, the labels overlap and thus become unreadable:

library(ggmap)
WPmap <- qmap(c(lon=4.80324, lat=52.40738), zoom = 12,  source = "google")

table kaart_rtw:

              Naam      lat     lon
1 Nieuw-Zeelandweg 52.40466 4.80214
2      Portsmuiden 52.39014 4.78554
3     Westhavenweg 52.41602 4.82282
4     Westhavenweg 52.41702 4.82282
5     Westhavenweg 52.41802 4.82282
6         Deccaweg 52.40196 4.83910
7     Coenhavenweg 52.40364 4.86195 

AmsterdamMap + geom_text(data = kaart_rtw, aes(label = kaart_rtw$Naam, x = X, y = Y)) 

Is there a way to stop the overlapping?

Mameluke answered 24/2, 2014 at 10:35 Comment(2)
Welcome to StackOverflow! Please read about how to provide a reproducible example, including data and code and then update your question accordingly.Taverner
I guess i just adjust the coordinates a bitMameluke
O
3

Adjusting the coordinates might be the easy solution, but only when you have few observations. An example:

df <- read.table(text="Naam lat lon
Nieuw-Zeelandweg 52.40466 4.80214
Portsmuiden 52.39014 4.78554
Westhavenweg 52.41602 4.82282
Westhavenweg 52.41702 4.82282
Westhavenweg 52.41802 4.82282
Deccaweg 52.40196 4.83910
Coenhavenweg 52.40364 4.86195", header = TRUE, strip.white = TRUE)

require(ggmap)
require(ggplot2)

roads <- get_map(location = c(lon = 4.82824, lat = 52.40738), zoom = 13, 
                 maptype = "roadmap", scale = 2)

ggmap(roads) +
  geom_point(data = df, aes(x=lon, y=lat, fill="red", alpha=0.5, label = df$Naam),
             size=4, shape=21) +
  geom_text(data = df, aes(x = lon, y = lat, label = Naam), 
            size = 3, vjust = 0, hjust = -0.1) +
  guides(fill = FALSE, alpha = FALSE)

The result: enter image description here

Owenism answered 26/2, 2014 at 21:14 Comment(0)
C
6

You might consider trying ggrepel to place your labels without overlaps:

library(ggmap)
install.packages("devtools")
devtools::install_github("slowkow/ggrepel")
library(ggrepel)

df <- read.table(text="Naam lat lon
Nieuw-Zeelandweg 52.40466 4.80214
Portsmuiden 52.39014 4.78554
Westhavenweg 52.41602 4.82282
Westhavenweg 52.41702 4.82282
Westhavenweg 52.41802 4.82282
Deccaweg 52.40196 4.83910
Coenhavenweg 52.40364 4.86195", header = TRUE, strip.white = TRUE)

roads <- get_map(location = c(lon = 4.82824, lat = 52.40738), zoom = 13, 
                 maptype = "roadmap", scale = 2)

ggmap(roads) +
  geom_point(
    data = df,
    aes(x = lon, y = lat),
    alpha = 0.5, fill = "red", size = 4, shape = 21
  ) +
  geom_label_repel(
    data = df,
    aes(x = lon, y = lat, label = df$Naam),
    box.padding = unit(2, "lines")
  ) +
  guides(fill = FALSE, alpha = FALSE)

ggmap plot with ggrepel labels

Catmint answered 4/2, 2016 at 14:13 Comment(1)
ggrepel is now on CRAN so can be installed directly from therePontius
O
3

Adjusting the coordinates might be the easy solution, but only when you have few observations. An example:

df <- read.table(text="Naam lat lon
Nieuw-Zeelandweg 52.40466 4.80214
Portsmuiden 52.39014 4.78554
Westhavenweg 52.41602 4.82282
Westhavenweg 52.41702 4.82282
Westhavenweg 52.41802 4.82282
Deccaweg 52.40196 4.83910
Coenhavenweg 52.40364 4.86195", header = TRUE, strip.white = TRUE)

require(ggmap)
require(ggplot2)

roads <- get_map(location = c(lon = 4.82824, lat = 52.40738), zoom = 13, 
                 maptype = "roadmap", scale = 2)

ggmap(roads) +
  geom_point(data = df, aes(x=lon, y=lat, fill="red", alpha=0.5, label = df$Naam),
             size=4, shape=21) +
  geom_text(data = df, aes(x = lon, y = lat, label = Naam), 
            size = 3, vjust = 0, hjust = -0.1) +
  guides(fill = FALSE, alpha = FALSE)

The result: enter image description here

Owenism answered 26/2, 2014 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.