改进居中县名 ggplot &地图 [英] Improve centering county names ggplot & maps

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

问题描述

早些时候我发布了一个关于使用 ggplot 和地图在地图上绘制县名的问题 ),实际上很简单(作为如果你使用 R 空间包 (sp),我花了几个小时到凌晨!).我测试了他们的一些其他函数来创建一个 SpatialPolygons 对象,您可以在该对象上使用 坐标 返回一个多边形质心.我只为一个县做了它,但是 Polygon (S4) 对象的 标签点 与质心匹配.假设这是真的,那么 Polygon 对象的标签点就是质心.我使用这个小过程来创建质心数据框并使用它们在地图上绘制.

library(ggplot2) # 用于 map_data.它只是一个包装器;应该只使用地图.图书馆(sp)图书馆(地图)getLabelPoint <- # 返回以县命名的标签点列表函数(县){多边形(县[c('long','lat')])@labpt}df <- map_data('county', 'new york') # 纽约地区县数据centroids <- by(df, df$subregion, getLabelPoint) # 返回列表centroids <- do.call("rbind.data.frame", centroids) # 转换为数据帧names(centroids) <- c('long', 'lat') # 适当的标题地图('县','纽约')文本(centroids$long,centroids$lat,rownames(centroids),offset=0,cex=0.4)

这不适用于每个多边形.通常,GIS 中的标注和注释过程要求您针对那些不适合您想要使用的自动(系统)方法的特殊情况调整标注和注释.我们对此采取的代码查看重新编码方法并不合适.最好包括检查给定图的给定大小的标签是否适合多边形;如果没有,请将其从文本标签的记录中删除,并稍后手动插入以适应情况——例如,添加引导线并注释到多边形的一侧,或将标签侧向显示在别处.

Early I posted a question about plotting county names on a map using ggplot and maps found HERE. My first approach was to take the means of all the lat and long coordinates per county as seen here:

Thankfully Andrie had 2 suggestions to improve the centering using a center of ranges and then the coord_map() {which appears to keep the aspect ratio correct}. This imporved the centering a great deal as seen here:

I think this looks better but still has some difficulties with overlap problems. I am hoping to further improve the centering (In that same thread Justin suggested a kmeans approach). I am ok with rotating text if necessary but am hoping for names that are centered and rotated if necessary (they extend beyond the county borders) to best display the county names on the map.

Any ideas?

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
p <- ggplot(ny, aes(long, lat, group=group)) +  geom_polygon(colour='black', fill=NA)

#my first approach to centering
cnames <- aggregate(cbind(long, lat) ~ subregion, data=ny, FUN=mean)
ggplot(ny, aes(long, lat)) +  
    geom_polygon(aes(group=group), colour='black', fill=NA) +
    geom_text(data=cnames, aes(long, lat, label = subregion), size=3)

#Andrie's much improved approach to centering
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=3) +
    coord_map()

解决方案

As I worked this out last night over at Talk Stats (link), it's actually pretty easy (as a product of the hours I spent into the early morning!) if you use the R spatial package (sp). I tested some of their other functions to create a SpatialPolygons object that you can use coordinates on to return a polygon centroid. I only did it for one county, but the label point of a Polygon (S4) object matched the centroid. Assuming this is true, then label points of Polygon objects are centroids. I use this little process to create a data frame of centroids and use them to plot on a map.

library(ggplot2)  # For map_data. It's just a wrapper; should just use maps.
library(sp)
library(maps)
getLabelPoint <- # Returns a county-named list of label points
function(county) {Polygon(county[c('long', 'lat')])@labpt}

df <- map_data('county', 'new york')                 # NY region county data
centroids <- by(df, df$subregion, getLabelPoint)     # Returns list
centroids <- do.call("rbind.data.frame", centroids)  # Convert to Data Frame
names(centroids) <- c('long', 'lat')                 # Appropriate Header

map('county', 'new york')
text(centroids$long, centroids$lat, rownames(centroids), offset=0, cex=0.4)

This will not work well for every polygon. Very often the process of labeling and annotation in GIS requires that you adjust labels and annotation for those peculiar cases that do not fit the automatic (systematic) approach you want to use. The code-look-recode approach we would take to this is not apt. Better to include a check that a label of a given size for the given plot will fit within the polygon; if not, remove it from the record of text labels and manually insert it later to fit the situation--e.g., add a leader line and annotate to the side of the polygon or turn the label sideways as was displayed elsewhere.

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

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