本文介绍了删除美国国家边界,在ggplot2 / geom_polygon中创建轮廓区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在美国制作了下面的多重耐药趋势表: b $ b 地图和数据都工作得很好,但我想删除州边界,并勾画出我描述的区域(资本-R数据集中的'Region'),所以地图看起来更像这样: 这些地图是不同的数据,所以解决方案不会匹配......并且我不担心轴标题或其他差异,只是区域边界。不过,我也想知道如何添加区域的标签,如上面的顶部/白色/空白地图。尝试添加一个geom_polygon图层并认为这是关键,但我无法让它勾勒出我创建的区域。谢谢大家的帮助!!希望数据集适用于dl ...请评论是否有更好的方式共享文件到SO。 在ggplot多边形结合由于某种原因没有为此工作,所以我从头开始使用一个单独的shapefile。 你会想改变一些或大部分美学。这仅仅是一个例子。 注意:您的数据确实需要一些清理(错误的名称和拼错的状态)。 library(grid) library(ggplot2) library(maptools) #library(ggthemes)#jlev14与pkg 图书馆(rgdal)图书馆(rgeos)图书馆(dplyr)图书馆(字符串) #在这里添加它与使用ggthemes,因为jlev14有问题与pkg theme_map< - function(base_size = 9,base_family =){ theme_bw(base_size = base_size,base_family = base_family)%+替换%theme(axis.line = element_blank( ),axis.text = element_blank(), axis.ticks = element_blank(),axis.title = element_blank(),panel.background = element_blank(),panel.border = element_blank(), panel.grid = element_blank(),panel.margin = unit(0,lines),plot.background = element_blank(),legend.justification = c(0, 0),legend.position = c 0,0))} #获取你的数据 ncftrendsort< - read.csv(〜/ Dropbox / mdrdata.csv,sep =,stringsAs = FALSE) #获得像样的美国地图 url< - http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_500k.json fil< - states.json if(!file.exists(fil))download.file (url,fil) #在地图中读入 us< - readOGR(fil,OGRGeoJSON,stringsAsFactors = FALSE)#过滤掉你没有的东西需要 us< - 美元[!(美元$%name%c(阿拉斯加,夏威夷,波多黎各)),] #可以更容易地合并 us @ data $ NAME< - tolower(us @ data $ NAME) #清理破损的数据 ncftrendsort< - mutate(ncftrendsort, region = ifelse (region ==washington,dc,columbia district, region)) ncftrendsort< - mutate(ncftrendsort, region = ifelse(region == louisana,louisiana,区域)) ncftrendsort< - filter(ncftrendsort,region!=hawaii) #与美国数据合并,因此我们可以合并区域 us @ data< ; - 合并(us @ data, distinct(ncftrendsort,region,Region), by.x =NAME,by.y =region,all.x = TRUE,sort = FALSE ) #区域联合会杀死数据帧,因此不会覆盖'us' regs< - gUnaryUnion(us,us @ data $ Region)#也可以使用在不简化多边形的情况下进行绘制 regs #将多边形正确地与名称相关联 nc_regs< - distinct(us @ data ,区域) regs< - SpatialPolygonsDataFrame(regs,nc_regs [c(2,1,4,5,3,6),],match.ID = FALSE) #get区域中心并添加文本的颜色,#仅指定第一年的范围,因此它仅绘制在一个方面 reg_labs< - mutate(add_rownames(as.data.frame(gCentroid(regs ,byi Region = gsub(,\ n,stri_trans_totitle(Region)), Years =1999-2003,color = c( 黑色,黑色,白色,黑色,黑色,黑色)) #准备好ggplot us_reg< - fortify(regs,region =Region) #获得国家概述和#仅指定第一年的范围,因此它只绘制在一个方面概述< - map_data(state)概述$ Years gg #填充区域 gg< ; --gg + geom_map(data = ncftrendsort,map = us_reg, aes(fill = mdr,map_id = Region), color =black,size = 0.5)#state outline只有在第一个方面 gg aes(x = long,y = lat,map_id = region), fill = #000000,color =#7f7f7f, linetype =dotted,size = 0.25,alpha = 0)#仅在第一个面上标注区域标签 gg< - gg + geom_text(data = reg_labs,aes(x = x,y = y ,label = Region), color = reg_labs $ color,size = 4) gg gg gg #你真的应该使用投影 gg< - gg + coord_map(albers,lat0 = 39,lat1 = 45) gg< - gg + theme_map( ) gg< - gg + theme(plot.title = element_text(size = 13,vjust = 2)) gg< - gg + theme(legend.position =right) $摆脱斜杠 gg< - gg + guides(fill = guide_legend(override.aes = list(color = NA))) gg I'm mapping the following table of multi-drug resistance trends in the US:MDR by regionUsing the following code:states_map<-map_data('state')m <- ggplot(ncftrendsort, aes(map_id = region)) + geom_map(aes(fill = ncftrendsort$mdr), map = states_map, color = 'black') + expand_limits(x = states_map$long, y = states_map$lat) + theme_classic() + scale_fill_continuous(name = "% MDR", low = 'white', high = 'black') + theme(axis.title.y = element_blank()) + theme(axis.title.x = element_blank()) + theme(axis.line = element_blank()) + theme(axis.ticks = element_blank()) + theme(axis.text.x = element_blank()) + theme(axis.text.y = element_blank()) + ggtitle('Regional Multi-Drug Resistant PSA (non-CF Patients), 1999-2012') + theme(plot.title = element_text(size = 13, vjust = 2)) + facet_grid(Years ~.)Which creates this:The map and data are all working just fine, but I want to remove the state borders and outline the regions I've delineated (capital-R 'Region' in dataset), so the map looks more like this:These maps are different data, so solutions won't match...and I'm not worried about the axes titles or other differences, just the regional borders. I would, however, also like to know how to add the labels of the regions like in the top/white/empty map above. Tried messing with adding a geom_polygon layer and think that's the key, but I can't get it to outline regions I've created. Thanks for your help everyone!! Hoping the dataset works for dl...please comment if there's a better way to share files to SO. 解决方案 The built-in ggplot polygon combine wasn't working for this for some reason, so I did it from scratch using a separate shapefile.You'll want to change some or most of the aesthetics. This is just an example.NOTE: Your data does need some cleaning (wrong names & misspelled state).library(grid)library(ggplot2)library(maptools)#library(ggthemes) # jlev14 was having issues with the pkglibrary(rgdal)library(rgeos)library(dplyr)library(stringi)# added it here vs use ggthemes since jlev14 was having issues with the pkgtheme_map <- function(base_size = 9, base_family = "") { theme_bw(base_size = base_size, base_family = base_family) %+replace% theme(axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), panel.background = element_blank(), panel.border = element_blank(), panel.grid = element_blank(), panel.margin = unit(0, "lines"), plot.background = element_blank(), legend.justification = c(0, 0), legend.position = c(0, 0))}# get your datancftrendsort <- read.csv("~/Dropbox/mdrdata.csv", sep=" ", stringsAs=FALSE)# get a decent US mapurl <- "http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_500k.json"fil <- "states.json"if (!file.exists(fil)) download.file(url, fil)# read in the mapus <- readOGR(fil, "OGRGeoJSON", stringsAsFactors=FALSE)# filter out what you don't needus <- us[!(us$NAME %in% c("Alaska", "Hawaii", "Puerto Rico")),]# make it easier to mergeus@data$NAME <- tolower(us@data$NAME)# clean up your broken datancftrendsort <- mutate(ncftrendsort, region=ifelse(region=="washington, dc", "district of columbia", region))ncftrendsort <- mutate(ncftrendsort, region=ifelse(region=="louisana", "louisiana", region))ncftrendsort <- filter(ncftrendsort, region != "hawaii")# merge with the us data so we can combine the regionsus@data <- merge(us@data, distinct(ncftrendsort, region, Region), by.x="NAME", by.y="region", all.x=TRUE, sort=FALSE)# region union kills the data frame so don't overwrite 'us'regs <- gUnaryUnion(us, us@data$Region)# takes way too long to plot without simplifying the polygonsregs <- gSimplify(regs, 0.05, topologyPreserve = TRUE)# associate the polygons to the names properlync_regs <- distinct(us@data, Region)regs <- SpatialPolygonsDataFrame(regs, nc_regs[c(2,1,4,5,3,6),], match.ID=FALSE)# get region centroids and add what color the text should be and# specify only the first year range so it only plots on one facetreg_labs <- mutate(add_rownames(as.data.frame(gCentroid(regs, byid = TRUE)), "Region"), Region=gsub(" ", "\n", stri_trans_totitle(Region)), Years="1999-2003", color=c("black", "black", "white", "black", "black", "black"))# make it ready for ggplotus_reg <- fortify(regs, region="Region")# get outlines for states and# specify only the first year range so it only plots on one facetoutlines <- map_data("state")outlines$Years <- "1999-2003"gg <- ggplot()# filled regionsgg <- gg + geom_map(data=ncftrendsort, map=us_reg, aes(fill=mdr, map_id=Region), color="black", size=0.5)# state outlines only on the first facetgg <- gg + geom_map(data=outlines, map=outlines, aes(x=long, y=lat, map_id=region), fill="#000000", color="#7f7f7f", linetype="dotted", size=0.25, alpha=0)# region labels only on first facetgg <- gg + geom_text(data=reg_labs, aes(x=x, y=y, label=Region), color=reg_labs$color, size=4)gg <- gg + scale_fill_continuous(name="% MDR", low='white', high='black')gg <- gg + labs(title="Regional Multi-Drug Resistant PSA\n(non-CF Patients), 1999-2012")gg <- gg + facet_grid(Years~.)# you really should use a projectiongg <- gg + coord_map("albers", lat0=39, lat1=45)gg <- gg + theme_map()gg <- gg + theme(plot.title=element_text(size=13, vjust=2))gg <- gg + theme(legend.position="right")# get rid of slashesgg <- gg + guides(fill=guide_legend(override.aes=list(colour=NA)))gg 这篇关于删除美国国家边界,在ggplot2 / geom_polygon中创建轮廓区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 03:57