rchoroplethchoroplethr

choroplethr legend not printing for some plots, problems with scale_fill_brewer()


I have two choropleth maps that I am attempting to make. The first one has worked fine. But when I replicate the code/method using a different value column, the legend does not print. The difference between the two sets of code lies in inserting scale_fill_brewer() with the second plot. Is using the scale_fill_brewer() overriding the c$legend?

For example, this works:

head(ra, 5)
           region value
    1     alabama   106
    2  california   622
    3    colorado    22
    4 connecticut    86
    5    delaware    43
 str(ra)
'data.frame':   51 obs. of  2 variables:
 $ region: chr  "alabama" "california" "colorado" "connecticut" ...
 $ value : num  106 622 22 86 43 7 232 19 10 121 ...

    c = StateChoropleth$new(ra)
    c$legend = "# of stores"
    c$set_num_colors(4)
    c$set_zoom(NULL)
    c$show_labels = FALSE
    without_abbr = c$render()
    without_abbr 

produces

but, the following results in "Value" in the legend:

head(ra, 5)
    region      value
    1    alabama  5.8703474
    2     alaska  0.4880526
    3    arizona  4.8851831
    4   arkansas  2.7045759
    5 california 35.2607419

    > str(ra)
    'data.frame':   51 obs. of  2 variables:

 $ region: chr  "alabama" "alaska" "arizona" "arkansas" ...
 $ value : num  5.87 0.488 4.885 2.705 35.261 ...

    c = StateChoropleth$new(ra)
    c$title = "Total Sales"
    c$legend = "$ billions"
    c$set_num_colors(4)
    c$set_zoom(NULL)
    c$show_labels = FALSE
    without_abbr = c$render()
    without_abbr + scale_fill_brewer(palette=2) # palette 2 is green

enter image description here


Solution

  • Thank you for using choroplethr.

    Try this:

    without_abbr + scale_fill_brewer(name="legend title", palette=2) 
    

    enter image description here

    The issue is with how ggplot2 handles legends (scales). You have the option of manually naming the scale/legend with the name parameter. Otherwise, it looks like it just uses the name of the column.