R,获取城市的经纬度数据并将其添加到我的数据框中。

5
我希望能从我的数据框中获取城市的经纬度数据,并将其作为两列添加到我的数据框中。我是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
我不会给你点踩,但在来这里问问题之前,你真的需要先尝试一下。向我们展示你的数据是一个好的开始。你具体尝试了什么?你有搜索过吗?例如:"在R中地理定位城市"。 - Simon O'Hanlon
请点击此链接查看:http://www.r-chart.com/2010/07/maps-geocoding-and-r-user-conference.html - Simon O'Hanlon
1个回答

12

关于您最初的问题 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)

这里输入图片描述


太好了,非常感谢……这将真正帮助我处理原始数据集!! - jonas
祝你好运,@jonas。除了繁琐的国家名称匹配之外,肯定还会有更多的绊脚石。 - lukeA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接