我正在使用如下所示的choroplethr地图。如何简单地删除州缩写?

下面是复制代码:
library(choroplethr)
library(choroplethrMaps)
data(df_pop_state)
df_pop_state$value <- as.numeric(df_pop_state$value)
state_choropleth(df_pop_state, num_colors = 1,
title = "2012 State Population Estimates",
legend = "Population")发布于 2020-04-07 01:16:15
感谢您使用choroplethr。请注意,Choroplethr使用R6对象。实际上,state_choropleth函数只是StateChoropleth R6对象的一个方便的包装器:
> state_choropleth
function (df, title = "", legend = "", num_colors = 7, zoom = NULL,
reference_map = FALSE)
{
c = StateChoropleth$new(df)
c$title = title
c$legend = legend
c$set_num_colors(num_colors)
c$set_zoom(zoom)
if (reference_map) {
if (is.null(zoom)) {
stop("Reference maps do not currently work with maps that have insets, such as maps of the 50 US States.")
}
c$render_with_reference_map()
}
else {
c$render()
}
}
<bytecode: 0x7fdda6aa3a10>
<environment: namespace:choroplethr>如果查看source code,您将看到对象上有一个字段可以执行您想要的操作:show_labels。默认为TRUE。
我们只需使用StateChoropleth对象(而不是函数)创建地图并将show_labels设置为FALSE,就可以获得您想要的结果。
c = StateChoropleth$new(df_pop_state)
c$title = "2012 State Population Estimates"
c$legend = "Population"
c$set_num_colors(1)
c$show_labels = FALSE
c$render()

我选择这种方法是因为,一般来说,我发现R中的许多函数都有大量的参数,这可能会让人感到困惑。缺点是函数比对象更容易文档化(特别是在R中),所以经常会出现这样的问题。
发布于 2020-04-05 15:15:16
此函数返回一个GeomText对象,因此您可以手动检查图层并删除不需要的图层(这里您想要删除ggplot层):
states <- state_choropleth(
df_pop_state,
num_colors = 1,
title = "2012 State Population Estimates",
legend = "Population"
)
layer_no <- grep("GeomText", sapply(states$layers, function(x) class(x$geom)[1]))
states$layers[[layer_no]] <- NULL
states

https://stackoverflow.com/questions/61038443
复制相似问题