ggplot在地图上居中显示名称

Tyl*_*ker 15 geocoding r ggplot2

我正在尝试使用ggplot2和地图来绘制纽约州的县名.我的方法是通过县找到纬度和经度的方法(我假设这是县的中心,但这可能是错误的思考),然后使用geom_text在地图上绘制名称.它没有像我预期的那样表现,因为它每个县都在绘制多个名字.

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

除了解决这个问题,我还很高兴能帮助理解我对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
Run Code Online (Sandbox Code Playgroud)

And*_*rie 27

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

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)
Run Code Online (Sandbox Code Playgroud)

注意:

  • 既然longlat出现在两个数据帧中,您可以aes(long, lat)在第一次调用ggplot时使用.您在此声明的任何映射都可用于所有图层.
  • 出于同样的原因,您需要aes(group=group)在多边形图层内声明.
  • 在文本层中,您需要将数据源移到外部aes.

完成后,地图绘制,你会发现中点更接近平均值range,并使用一个尊重纵横比和投影的地图坐标系:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Nat*_*rth 6

我知道这是一个已经回答的老问题,但我想添加这个以防万一有人在这里寻找未来的帮助。

maps 包具有map.text使用多边形质心放置标签的功能。查看它的代码,可以看到它使用apply.polygoncentroid.polygon函数来查找质心。这些函数在加载包时不可见,但仍然可以访问:

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)

# Use the map function to get the polygon data, then find the centroids
county_poly <- map("county", "new york", plot=FALSE, fill = TRUE)
county_centroids <- maps:::apply.polygon(county_poly, maps:::centroid.polygon)

# Create a data frame for graphing out of the centroids of each polygon
# with a non-missing name, since these are the major county polygons.
county_centroids <- county_centroids[!is.na(names(county_centroids))]
centroid_array <- Reduce(rbind, county_centroids)
dimnames(centroid_array) <- list(gsub("[^,]*,", "", names(county_centroids)),
                                 c("long", "lat"))
label_df <- as.data.frame(centroid_array)
label_df$county <- rownames(label_df)

p <- ggplot(ny, aes(long, lat, group=group)) + geom_polygon(colour='black', fill=NA) 

plabels <- geom_text(data=label_df, aes(label=county, group=county))
p + plabels
Run Code Online (Sandbox Code Playgroud)