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

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

问题描述

我试图用ggplot2和地图来绘制纽约州县名。我的方法是找到县的经纬度方法(我认为这是县的中心,但这可能是错误的想法),然后使用geom_text来绘制地图上的名称。这不符合我的预期,因为它正在绘制每个县的多个名称。



我要查找的结果是每个文本(县)的中心位于中心它是各自的县。



除了解决这个问题,我希望能帮助理解我在ggplot上的想法。



预先感谢您。

  library(ggplot2);图书馆(地图)

county_df< - map_data('county')各州按县划分的#mappings
ny< - subset(county_df,region ==new york)#subset仅用于NYS
ny $ county < - ny $ subregion
cnames < - 集合(cbind(long,lat)〜subregion,data = ny,FUN = mean)

p <-ggplot(ny,aes(long,lat,group = group))+ geom_polygon(color ='black',fill = NA)
p #p当然如预期的图

#现在添加一些县名(3次错误的尝试)
p + geom_text(aes(long,lat,data = cnames,label = subregion,size = .5))#不正确

#我说可能我把它与相同的名称混淆了不同的数据集
名称(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)尝试3


解决方案

多边形a nd第二个标签),你需要为每一层指定数据源和映射: aes(long,lat))+
geom_polygon(aes(group = group),color ='black',fill = NA)+
geom_text(data = cnames,aes(long,lat,label =分区域),size = 2)

注意:

由于 long lat 在两个数据框中都会出现,所以您可以使用 aes(long,lat)在第一次调用ggplot。您在这里声明的任何映射都可用于所有图层。

  • 出于同样的原因,您需要声明 aes(group = group)在文本图层中,您需要将数据源移动到 aes 之外。



  • 一旦你完成了这些工作并绘制了地图,你会发现中点更接近的平均值,范围,并使用尊重长宽比和投影的地图坐标系:

      cnames< ; cbind(long,lat)〜subregion,data = ny,
    FUN = function(x) ,lat))+
    geom_polygon(aes(group = group),color ='black',fill = NA)+
    geom_text(data = cnames,aes(long,lat,label = subregion), size = 2)+
    coord_map()


    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.

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

    Thank you in advance.

    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)
    

    Note:

    • 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.

    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天全站免登陆