我应该什么时候使用geom_map? [英] When should I use geom_map?
问题描述
我正在ggplot中添加点的choropleth地图。
因此,遵循 geom_map帮助文档的最后一个示例
我想出了这个:
require(ggplot2)
要求(地图)
set.seed(47)
county_map< - map_data(county,washington)
名称(county_map)[5:6 ] <-c(state,id)
countyData< - data.frame(id = unique(county_map $ id),value = rnorm(39))
map1< ggplot(countyData,aes(map_id = id))+
geom_map(aes(fill = value),map = county_map,color =black)+
coord_map()+
expand_limits x = county_map $ long,y = county_map $ lat)
print(map1)
这对于等值线地图非常有用。 (除此之外,我对 map_data
函数感到兴奋。)但是,我尝试并添加点数
pre > pointData< - structure(list(xx = c(-119.872483243387,-122.809038239929,
-122.143143065312
),yy = c(48.1320425447619,46.7352071436801,47.9911548514037
)),.Names = c(xx,yy),row.names = c(1746L,7281L,2692L
),class =data.frame)
map1 + geom_point(mapping = aes(xx,yy),data = pointData)
让它工作。我尝试了很多变化,将 group
设置为 NULL
,命名为 aes
参数等。没有运气。所以我找到这个问题,它通过合并地图数据和choropleth数据并使用 geom_polygon
,这看起来更直接。 (我花了一些时间来首先计算ID映射,因为我没有意识到必须删除 region
列名才能成功使用<$ c $ b> b
$ b
所以,有两个问题:
- 如何使用上面显示的方法使用
geom_map
添加来自不同数据框的点。 ?
- 更重要的是,使用
geom_map
与使用geom_polygon
方法?
- 更重要的是,使用
您的直接问题是 ggplot
无法将您的点数据与地图绑定。看看你的数据框架,你有这样的地图:
str(countyData)
'data.frame' :39 obs。 2个变量:
$ id:chradamsasotinbentonchelan...
$值:num 1.995 0.711 0.185 -0.282 0.109 ...
...以下是您的要点:
str(pointData)
'data.frame':3 obs。 2个变量:
$ xx:num -120 -123 -122
$ yy:num 48.1 46.7 48
您是否看到有哪些常用变量可以让 ggplot
找到您的积分?
<不过,这个问题很容易解决。我通常使用
geom_polygon
而不是 geom_map
,但这很大程度上没有习惯。例如: colnames(pointData)< - c('long','lat')#使一致(1:nrow(county_map),
函数(x)round(())(
pointData $ group< - 1#ggplot需要一个组来处理
county_map $ value< - sapply runif(1,1,8),0))#颜色
ggplot(县地图,aes(x = long,y = lat,group = group))+
geom_polygon(aes (fill = value))+
coord_map()+
geom_point(data = pointData,aes(x = long,y = lat),shape = 21,fill =red)
给出以下内容(注意点)。
然而,至于你是否应该使用 geom_map
或 geom_polygon
,我没有真正考虑过这个问题。也许别人有看法。
I'm making a choropleth map with added points in ggplot. So, following the last example of the geom_map help docs
I came up with this:
require(ggplot2)
require(maps)
set.seed(47)
county_map <- map_data("county", "washington")
names(county_map)[5:6] <- c("state", "id")
countyData <- data.frame(id = unique(county_map$id), value = rnorm(39))
map1 <- ggplot(countyData, aes(map_id = id)) +
geom_map(aes(fill = value), map = county_map, colour = "black") +
coord_map() +
expand_limits(x = county_map$long, y = county_map$lat)
print(map1)
which works great for the choropleth map. (Aside that I'm thrilled with the map_data
function.) But then I try and add points
pointData <- structure(list(xx = c(-119.872483243387, -122.809038239929,
-122.143143065312
), yy = c(48.1320425447619, 46.7352071436801, 47.9911548514037
)), .Names = c("xx", "yy"), row.names = c(1746L, 7281L, 2692L
), class = "data.frame")
map1 + geom_point(mapping = aes(xx, yy), data = pointData)
And I can't get it to work. I tried a lot of variations, setting group
to NULL
, naming aes
arguments, etc. No luck. So I find this question which does the exact same thing without a problem by merging the map data with the choropleth data and using geom_polygon
, which seems more straightforward anyway. (It took me a little while to work out the ID mapping in the first place because I didn't realize I had to remove the region
column name to successfully use id
. And the syntax of the first method still seems weird to me.)
So, two questions:
- How is it possible to add points from a different data frame using the method shown above with
geom_map
? - More importantly, are there any advantages to using
geom_map
as opposed to thegeom_polygon
approach?
Your immediate problem is that ggplot
has no way to tie your point data to the map. Looking at your data frames, you have this for your map:
str(countyData)
'data.frame': 39 obs. of 2 variables:
$ id : chr "adams" "asotin" "benton" "chelan" ...
$ value: num 1.995 0.711 0.185 -0.282 0.109 ...
...and this for your points:
str(pointData)
'data.frame': 3 obs. of 2 variables:
$ xx: num -120 -123 -122
$ yy: num 48.1 46.7 48
Do you see any common variables there that would allow ggplot
to locate your points?
Still, the problem is easily resolved. I typically use geom_polygon
rather than geom_map
but that's largely out of habit. This works, for example:
colnames(pointData) <- c('long','lat') # makes consistent with county_map
pointData$group <- 1 # ggplot needs a group to work with
county_map$value <- sapply(1:nrow(county_map),
function(x) round(runif(1, 1, 8), 0)) # for colours
ggplot(county_map, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = value)) +
coord_map() +
geom_point(data = pointData, aes(x = long, y = lat), shape = 21, fill = "red")
Which gives the following (note the points).
However, as to whether you should use geom_map
or geom_polygon
, I have not really thought about the issue much. Maybe somebody else has a view.
这篇关于我应该什么时候使用geom_map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!