2

I'm trying to change the color of my statebins map. I'm super new to R and still learning a lot, and googling around hasn't really helped.

This is what the sample looks like:

   fipst stab                state color
1     37   NC       North Carolina     2
2      1   AL              Alabama     2
3     28   MS          Mississippi     2
4      5   AR             Arkansas     2
5     47   TN            Tennessee     2
6     45   SC       South Carolina     1
7     23   ME                Maine     2
49    32   NV               Nevada     1
50    15   HI               Hawaii     2
51    11   DC District of Columbia     2

 digitaltax <- structure(list(fipst = c(37L, 1L, 28L, 5L, 47L, 45L, 23L, 32L,15L, 11L), stab = c("NC", "AL", "MS", "AR", "TN", "SC", "ME","NV", "HI", "DC"), state = c("North Carolina", "Alabama", "Mississippi","Arkansas", "Tennessee", "South Carolina", "Maine", "Nevada","Hawaii", "District of Columbia"), color = c(2L, 2L, 2L, 2L,2L, 1L, 2L, 1L, 2L, 2L)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,7L, 49L, 50L, 51L), class = "data.frame") 

    ==X==============================================================X==

        mutate(
      digitaltax, 
      share = cut(color, breaks = 3, labels = c("No sales tax", "Exempts digital goods", "Taxes digital goods"))
    ) %>% 
      statebins(
        value_col = "share", font_size = 2.5,
        ggplot2_scale_function = scale_fill_brewer,
        name = ""
      ) +
    labs(title = "Which states tax digital products?") + theme_statebins()

This produces a map with a range of blues. How can I change the color? No matter what I've tried and found on google, it always throws this error:

Error in ggplot2_scale_function(...) : could not find function "ggplot2_scale_function"

Any help at all would be super appreciated. Thank you!

kmm
  • 51
  • 3
  • 1
    Hi Kathy, welcome to Stack Overflow. `ggplot2_scale_function` is not a function. Perhaps you were reading somewhere that suggested you used a scale function from ggplot2? If you can provide at least a sample of `digitaltax`, we can likely be of assistance. You can try `dput(digitaltax)`. You can edit your question and paste the output. You can surround it with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell May 12 '20 at 19:10
  • For the colors, try using [one of the color palettes here](https://www.datanovia.com/en/blog/the-a-z-of-rcolorbrewer-palette/). I'm not familiar with `statebins()`, but in the documentation it looks like there's an option to specify colors like `brewer_pal = "PuBu" ` – 4redwood May 12 '20 at 19:17
  • 1
    @Ian Campbell, I've added a sample - thank you for all of that important info. I hope that helps! – kmm May 12 '20 at 19:33
  • 1
    It took me forever to figure out that you're using the development version of `statebins`. In the future, if you're using a version that is not in CRAN, please mention that in the question. – Ian Campbell May 12 '20 at 20:14
  • Sorry @Ian-Campbell I saw the github version had the parameter she was using and went there first. – Chuck P May 12 '20 at 20:25
  • Kathy if that solved your problem please mark the answer correct – Chuck P May 12 '20 at 21:35

2 Answers2

1

With your data and most of your code I did change DC color to 3 so it shows all categories.

library(dplyr)
library(ggplot2)
library(statebins)

# changes DC to be color 3
digitaltax <- structure(list(fipst = c(37L, 1L, 28L, 5L, 47L, 45L, 23L, 32L,15L, 11L), stab = c("NC", "AL", "MS", "AR", "TN", "SC", "ME","NV", "HI", "DC"), state = c("North Carolina", "Alabama", "Mississippi","Arkansas", "Tennessee", "South Carolina", "Maine", "Nevada","Hawaii", "District of Columbia"), color = c(2L, 2L, 2L, 2L,2L, 1L, 2L, 1L, 2L, 3L)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,7L, 49L, 50L, 51L), class = "data.frame")

mutate(
  digitaltax, 
  share = cut(color, breaks = 3, labels = c("No sales tax", "Exempts digital goods", "Taxes digital goods"))
) %>% 
  statebins(
    value_col = "share", font_size = 2.5,
    ggplot2_scale_function = scale_fill_brewer,
    name = ""
  ) +
  labs(title = "Which states tax digital products?") + 
  theme_statebins()

Created on 2020-05-12 by the reprex package (v0.3.0)

Chuck P
  • 3,862
  • 3
  • 9
  • 20
  • 1
    thank you for taking the time to help. Yes, this is basically the map that I get with the range of blues. Were you able to change the color palette somehow? I have added my data as well. – kmm May 12 '20 at 19:42
  • It was `ggplot2_scale_function = ggplot2::scale_fill_distiller` that was the issue one sec and I'll use your data – Chuck P May 12 '20 at 19:54
1

One approach is to use named RColorBrewer palates with brewer_pal =:

library(statebins)
statebins(digitaltax,
          value_col = "color",
          breaks = length(unique(digitaltax$share)),
          labels = unique(digitaltax$share),
          brewer_pal = "Dark2") +
  labs(title = "Which states tax digital products?") 

enter image description here

Execute this command to see all palates:

library(RColorBrewer)
display.brewer.all()

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57