ggplot 在地图上居中的名称 [英] ggplot centered names on a map

查看:34
本文介绍了ggplot 在地图上居中的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ggplot2 和地图来绘制纽约州县的名称.我的方法是按县找到经纬度的平均值(我假设这是县的中心,但这可能是错误的想法),然后使用 geom_text 在地图上绘制名称.它的行为不像我预期的那样,因为它为每个县绘制了多个名称.

I'm attempting to use ggplot2 and maps to plot the names of the counties in NY state. My approach was to find the means of latitude and longitude by county (I assume this is the center of the county but this may be faulty thinking) and then use geom_text to plot the names on the map. It's not behaving as I anticipated as it's plotting multiple names per county.

我正在寻找的结果是每个文本(县)的中心位于其各自县的中心.

The outcome I'm looking for is that the center of each text (county) is at the center of it's respective county.

除了解决问题之外,我很乐意帮助您了解我对 ggplot 的想法有什么问题.

In addition to solving the problem I'd appreciate helping to understand what's wrong with my thinking with ggplot.

提前致谢.

library(ggplot2); library(maps)

county_df <- map_data('county')  #mappings of counties by state
ny <- subset(county_df, region=="new york")   #subset just for NYS
ny$county <- ny$subregion
cnames <- aggregate(cbind(long, lat) ~ subregion, data=ny, FUN=mean)

p <- ggplot(ny, aes(long, lat, group=group)) +  geom_polygon(colour='black', fill=NA) 
p #p of course plots as expected

#now add some county names (3 wrong attempts)
p + geom_text(aes(long, lat, data = cnames, label = subregion, size=.5)) #not correct

#I said maybe I'm confusing it with the same names for different data sets
names(cnames) <-c('sr', 'Lo', 'La')
p + geom_text(Lo, La, data = cnames, label = sr, aes(size=.5)) #attempt 2
p + geom_text(aes(Lo, La, data = cnames, label = sr, size=.5)) #attempt 3

推荐答案

由于您要创建两个图层(一个用于多边形,第二个用于标签),您需要为每个图层指定数据源和正确映射:

Since you are creating two layers (one for the polygons and the second for the labels), you need to specify the data source and mapping correctly for each layer:

ggplot(ny, aes(long, lat)) +  
    geom_polygon(aes(group=group), colour='black', fill=NA) +
    geom_text(data=cnames, aes(long, lat, label = subregion), size=2)

注意:

  • 由于 longlat 出现在两个数据帧中,您可以在第一次调用 ggplot 时使用 aes(long, lat).您在此处声明的任何映射都可用于所有图层.
  • 出于同样的原因,您需要在多边形层内声明aes(group=group).
  • 在文本层,需要将数据源移到aes之外.
  • Since long and lat occur in both data frames, you can use aes(long, lat) in the first call to ggplot. Any mapping you declare here is available to all layers.
  • For the same reason, you need to declare aes(group=group) inside the polygon layer.
  • In the text layer, you need to move the data source outside the aes.

完成此操作后,您会发现通过range 的平均值更好地近似中点,并使用尊重纵横比的地图坐标系和投影:

Once you've done that, and the map plots, you'll realize that the midpoint is better approximated by the mean of range, and to use a map coordinate system that respects the aspect ratio and projection:

cnames <- aggregate(cbind(long, lat) ~ subregion, data=ny, 
                    FUN=function(x)mean(range(x)))

ggplot(ny, aes(long, lat)) +  
    geom_polygon(aes(group=group), colour='black', fill=NA) +
    geom_text(data=cnames, aes(long, lat, label = subregion), size=2) +
    coord_map()

这篇关于ggplot 在地图上居中的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆