2014-01-05 17 views
3

我想獲取我的數據框中城市的經度/緯度數據,並在我的框架中添加爲2列。我是R新手,我不知道該怎麼做。 有人能幫助我,這R,獲取城市的經度/緯度數據並將其添加到我的數據框中

我的框架:

> data <- read.xlsx("example_city.xlsx", 1) 
> data 
     City Country 
1 Stockholm Sweden 
2  Oslo Norway 
3  Rome Italy 
4  Rome Italy 
5 Stockholm Sweden 
6 Stockholm Sweden 
7  Paris France 
8  Paris France 
9 Hamburg Germany 
10  Paris France 
11  Paris France 
+2

我不會downvote你,但你真的在問這裏之前,首先需要做一些研究。向我們展示您的數據是一個好的開始。你有什麼特別的嘗試?你有沒有搜索引擎任何這個?例如*「R在地理定位城市」*。 –

+0

Here you go ... http://www.r-chart.com/2010/07/maps-geocoding-and-r-user-conference.html –

回答

10

參考你原來的問題https://stackoverflow.com/questions/20936263/use-ggplot2-to-plot-cities-on-a-map

# data 
cities <- sort(c(rep('Stockholm', 3), 'Oslo', 'Rome', 'Rome', 'Paris', rep('Bonn',10), 'Paris', 'Paris', 'Stockholm')) 

# get frequencies 
freq <- as.data.frame(table(cities)) 
library(plotrix) 
freq$Freq <- rescale(freq$Freq, c(1,10)) # c(scale_min, scale_max) 

# get cities latitude/longitude - kindly provided by google: 
library(ggmap) 
lonlat <- geocode(unique(cities)) 
cities <- cbind(freq, lonlat) 

# get matches between names {maps} names and EU country names 
library(maps) 
eu <- c("Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czech Republic", 
     "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", 
     "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", 
     "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", 
     "Slovenia", "Spain", "Sweden", "United Kingdom") 
warning("No matches in database for ", paste(setdiff(eu, map_data('world')$region), collapse=", ")) 
europe <- map_data('world', region=eu) 

# plot 
library(ggplot2) 
ggplot(europe, aes(x=long, y=lat, group=group)) + 
    geom_polygon(fill="white", colour="black") + 
    xlim(-20, 40) + ylim(25,75) + 
    geom_point(data=cities, inherit.aes=F, aes(x=lon, y=lat, size=Freq), colour="red", alpha=.8) + 
    geom_text(data=cities, inherit.aes=F, aes(x=lon, y=lat, label=cities), vjust=1, colour="red", alpha=.5) 

enter image description here

+0

太好了,非常感謝你...這真的會有幫助我與我的原始數據集! – jonas

+0

祝你好運,@jonas。除了繁瑣的國名匹配之外,肯定會有更多的絆倒危險。 – lukeA

相關問題