When should I use geom_map?

℡╲_俬逩灬. 提交于 2019-12-06 02:59:32

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.

agstudy

This works for me :

map1 <- ggplot(countyData) +
  geom_map( map = county_map, aes(map_id = id,fill = value), 
            colour = "black") + coord_map() +
  expand_limits(x = county_map$long, y = county_map$lat)
  map1 + geom_point(mapping = aes(xx, yy), data = pointData)

For me geom_map is a wrapper of a geom_polygon. It is a layer that contains all geographical settings (lat and long grouped by id).

I would use geom_map when I plot a map and geom_polygon to plot any polygon type.

EDIT To add the map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!