我正在绘制美国多药耐药趋势的下表:
使用以下代码:
states_map< -map_data( '州')
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 ~.)
地图和数据都运行得很好,但我想删除状态边框并勾勒出我所描绘的区域(资本-R'Region'在数据集中),因此地图看起来更像是这样:{{ 3}}
这些地图是不同的数据,所以解决方案不匹配......我并不担心轴标题或其他差异,只是区域边界。但是,我也想知道如何在上面的顶部/白色/空地图中添加区域的标签。尝试添加一个geom_polygon图层并认为这是关键,但我无法用它来概述我创建的区域。谢谢大家的帮助!!希望数据集适用于dl ...请评论是否有更好的方式将文件共享到SO。
答案 0 :(得分:17)
由于某些原因,内置的ggplot多边形组合无法正常工作,因此我使用单独的shapefile从头开始。
你会想要改变一些或大部分的美学。这只是一个例子。
注意:您的数据确实需要一些清理(错误的名称和拼写错误的状态)。
library(grid)
library(ggplot2)
library(maptools)
#library(ggthemes) # jlev14 was having issues with the pkg
library(rgdal)
library(rgeos)
library(dplyr)
library(stringi)
# added it here vs use ggthemes since jlev14 was having issues with the pkg
theme_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 data
ncftrendsort <- read.csv("~/Dropbox/mdrdata.csv", sep=" ", stringsAs=FALSE)
# get a decent US map
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)
# read in the map
us <- readOGR(fil, "OGRGeoJSON", stringsAsFactors=FALSE)
# filter out what you don't need
us <- us[!(us$NAME %in% c("Alaska", "Hawaii", "Puerto Rico")),]
# make it easier to merge
us@data$NAME <- tolower(us@data$NAME)
# clean up your broken data
ncftrendsort <- 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 regions
us@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 polygons
regs <- gSimplify(regs, 0.05, topologyPreserve = TRUE)
# associate the polygons to the names properly
nc_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 facet
reg_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 ggplot
us_reg <- fortify(regs, region="Region")
# get outlines for states and
# specify only the first year range so it only plots on one facet
outlines <- map_data("state")
outlines$Years <- "1999-2003"
gg <- ggplot()
# filled regions
gg <- 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 facet
gg <- 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 facet
gg <- 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 projection
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")
# get rid of slashes
gg <- gg + guides(fill=guide_legend(override.aes=list(colour=NA)))
gg