简体   繁体   中英

How to Remove State Abbreviations from a Choroplethr Map

I'm working with a choroplethr map like the one below. How do I simply remove the state abbreviations?

enssster 图像描述在这里

Here is the replication code:

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")

Thank you for using choroplethr. Note that Choroplethr uses R6 Objects. In fact, the state_choropleth function is just a convenience wrapper for the StateChoropleth R6 object:

> 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>

If you look at the source code you will see that there is a field on the object that does what you want: show_labels . It defaults to TRUE .

We can get the result you want by simply creating your map using the StateChoropleth object (not the function) and setting show_labels to 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()

在此处输入图像描述

I chose this approach because, in general, I found that many functions in R have a large number of parameters, and that can be confusing. The downside is that functions are easier to document than objects (especially in R), so questions like this frequently come up.

This function returns a ggplot object, so you can manually inspect the layers and remove the one you don't want (here you want to remove the GeomText layer):

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM