如何从Choroplethr映射中删除状态缩写

问题描述 投票:1回答:2

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

enssster image description here

这里是复制代码:

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")
r ggplot2 maps choroplethr
2个回答
1
投票

感谢您使用胆囊。请注意,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()

enter image description here

我之所以选择这种方法,是因为一般而言,我发现R中的许多函数都有大量参数,这可能会造成混淆。缺点是函数比对象(尤其是R语言)更容易记录文档,因此经常出现类似问题。


0
投票

此函数返回一个ggplot对象,因此您可以手动检查图层并删除不需要的图层(这里要删除GeomText图层):

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.