如何将数据框中的信息添加到R中的美国地图中?

时间:2014-03-24 01:19:36

标签: r dataframe

我有一个基于R的全球恐怖主义数据库gtd的数据框。

我已下载/需要地图包

我要做的是从我的gtd数据框中获取数据并在美国地图上添加这些点。

我知道美国地图的代码是map("state")

在我的gtd数据框中,有一个纬度和经度列

我想做的就是在美国地图上绘制点,以显示在美国发生恐怖袭击的位置

所以从我的gtd数据库中只得到一个需要美国经度/纬度的子集我知道我会用

subset(x=gtd, country_txt=="United States")

但是从那里开始,如何在美国地图上绘制攻击位置?

非常感谢!

1 个答案:

答案 0 :(得分:1)

我将从您帖子中的线索中做出广泛的假设,即您正在使用global terrorism database中的数据。如果您已经设法从中读取了大量的CSV,那么您想要做的就应该工作,而您真的很接近答案。基本上,您只需要将points()添加到map()

library(maps)

# assuming you have it in a CSV file
dat <- read.csv("gtd.csv")

# subset only points in the US
US <- dat[dat$country_txt == "United States",]

# plot the base US map
map('state')

# add points with really horrid default color, point type & size
# changing this is a good exercise for the poster :-)
points(x=US$longitude, y=US$latitude, col="red", cex=0.75)

map

这与ggplot2无关,因此请考虑删除该标记。可以使用ggplot完成,但maps()不使用它。