首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Choroplethr映射中删除状态缩写

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

Stack Overflow用户
提问于 2020-04-05 13:28:40
回答 2查看 191关注 0票数 1

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

下面是复制代码:

代码语言:javascript
复制
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")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-07 01:16:15

感谢您使用choroplethr。请注意,Choroplethr使用R6对象。实际上,state_choropleth函数只是StateChoropleth R6对象的一个方便的包装器:

代码语言:javascript
复制
> 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,就可以获得您想要的结果。

代码语言:javascript
复制
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中),所以经常会出现这样的问题。

票数 1
EN

Stack Overflow用户

发布于 2020-04-05 15:15:16

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

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61038443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档