gpt4 book ai didi

删除美国州界,在 ggplot2/geom_polygon 中创建轮廓区域

转载 作者:行者123 更新时间:2023-12-04 01:51:13 24 4
gpt4 key购买 nike

我正在绘制下表的美国多重耐药性趋势:

MDR by region

使用以下代码:

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 ~.)

这创造了这个: MDRstates

map 和数据都工作得很好,但我想删除州边界并勾勒出我划定的区域(数据集中的大写-R“区域”),因此 map 看起来更像这样: MDRregions

这些 map 是不同的数据,所以解决方案不会匹配......我不担心轴标题或其他差异,只是区域边界。但是,我也想知道如何添加区域的标签,例如上面的顶部/白色/空 map 。尝试添加 geom_polygon 图层并认为这是关键,但我无法勾勒出我创建的区域。谢谢大家的帮助!!希望数据集适用于 dl...如果有更好的方式将文件共享给 SO,请发表评论。

最佳答案

由于某种原因,内置的 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

enter image description here

关于删除美国州界,在 ggplot2/geom_polygon 中创建轮廓区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com