3

I am using the usmap library which seems to do a great job of mapping counties in R. However, I have 1 issue, the legend titles for maps of factors.

Using name in scale_color_gradient(low="green",high="darkgreen",name="My Factor") doesn't work. Interestingly, it works in scale_fill_continuous(low="green", high="darkred", guide="colorbar",na.value="lightgray",name = "My Measure")

good legend bad legend

Thoughts?

This reproduces the problem:

require(usmap)
require(ggplot2)

d=data.frame(fips=as.integer( c(10001 ,10003 ,10005,  1001 , 1003 , 1005)))
d$measure=d$fips

plot_usmap(regions = 'counties',data=d, value='measure',lines=NA)+
  scale_fill_continuous(low="green", high="darkred", 
                        guide="colorbar",na.value="lightgray",name = "My Measure")+
  ggtitle('Legend title is correct')

d$measure=as.factor(d$fips) #simulate a factor
plot_usmap(regions = 'counties',data=d, value='measure',lines=NA)+
  scale_color_gradient(low="green",high="darkgreen",name="My Factor")+
  ggtitle('Notice the legend title is not correct')+
  theme(legend.position="bottom") 
Chris
  • 1,219
  • 2
  • 11
  • 21
  • The colours of the counties will be a `fill` aesthetic, not a `color`, which is why your first version works and your second doesn't (i.e. you are setting a name for an aesthetic that does not exist). – Andrew Gustar Nov 26 '17 at 15:20
  • I get 'Error: Discrete value supplied to continuous scale' when I try scale_fill_gradient – Chris Nov 26 '17 at 15:24
  • Not if you don't run the line `d$measure=as.factor(d$fips)` - that is setting `measure` to be discrete. – Andrew Gustar Nov 26 '17 at 15:30
  • Andrew Gustar, this is example code. In the real-thing, I have a discrete variable. I need to change the legend title of a discrete variable. – Chris Nov 26 '17 at 15:32
  • 1
    Try `plot_usmap(regions = 'counties',data=d, values='measure',lines=NA) + scale_fill_discrete(name="My Factor")`. – Andrew Gustar Nov 26 '17 at 15:52
  • @AndrewGustar, scale_fill_discrete works but I wish it was simpler to use e.g. low="green", high="darkred" – Chris Nov 27 '17 at 14:02
  • 2
    scale_fill_manual(values = c(colorRampPalette(c("green", "red"))( 6 )),name= "My name") works great! – Chris Nov 27 '17 at 14:06

0 Answers0