11

I have put together the Shiny app (below), showing a choropleth, which looks good except for the scientific notation on the legend.

I would like the values to read: '$5,000,000', and '$4,000,000', etc, etc.

enter image description here

The code boils down to

g <-choroplethr(DF, lod="state", num_buckets = 1)
g <- g + scale_fill_gradient(high = "springgreen4", low= "grey90", name="Sum")

I attempted this:

g <- q + scale_fill_gradient(high = "springgreen4", low= "grey90", name="Sum", labels = c("5,000,000", "4,000,000", "3,000,000", "2,000,000", "1,000,000"))  

But I got the error, Breaks and labels are different lengths

I'm not sure how I can specify breaks on the x axis when I'm dealing with a map? How can I create breaks that work with the labels I want to include? Thanks.

Ari
  • 1,819
  • 14
  • 22
tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149

1 Answers1

22

If you are adding argument labels= to scale_fill_gradient() then you should also add argument breaks= that is the same length as your labels.

+ scale_fill_gradient(high = "springgreen4", low= "grey90", name="Sum", 
          labels = c("5,000,000", "4,000,000", "3,000,000", "2,000,000", "1,000,000"),
           breaks = c(5000000, 4000000, 3000000, 2000000, 1000000))

In this case a better solution would be to just use the dollar format from the scales library. That will automatically add dollar signs and commas to the labels.

library(scales)
+ scale_fill_gradient(labels=dollar)
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • 3
    in addition to above for dollar format, this could work `labels = paste0("$",c("5,000,000", "4,000,000", "3,000,000", "2,000,000", "1,000,000"))`, – Silence Dogood May 08 '14 at 07:20