2014-02-24 142 views
4

我擁有Google地圖和帶有文本標籤的座標列表。當我預覽此,標籤重疊,因而成爲不可讀:重疊標籤ggmap

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

表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)) 

有沒有辦法阻止重疊?

+1

歡迎的StackOverflow!請閱讀如何提供[一個可重複的例子,包括數據和代碼](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),然後更新您的問題因此。 – Thomas

+0

我想我只是調整座標 – user3346225

回答

3

調整座標可能是簡單的解決方案,但只有當你沒有觀察到時。舉個例子:

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) 

結果: enter image description here

2

你可能會考慮嘗試ggrepel把你的標籤不重疊:

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

+0

ggrepel現在在CRAN上,所以可以直接從那裏安裝 –