Flip diverge colors

182 views
Skip to first unread message

Boris Shor

unread,
Mar 19, 2012, 2:20:25 PM3/19/12
to ggp...@googlegroups.com

Hello,

I'm enjoying easy creation of choropleth plots with ggplot 0.8.9 on R2.13. In particular, I'm making use of the diverge color map, drawing from Hadley's examples. I really like the red-to-blue one, but I can't seem to figure out how to flip high and low with it (like I can without a diverge gradient). In the following plot, I'd like the red end to be blue (eg California), and the blue end to be red.

Here's my code: 

choropleth.st (value, name="Avg Chamber Polarization", main = "State Legislative Polarization 2006", filename = "geography_avg_polarization", diverge=T)

choropleth.st <- function (value, name, main, filename, place, diverge=F) {
  
  library(ggplot2)
  library(maps)
  library(mapproj)
  
  ak.hi = which(names(value)%in%c("AK","HI"))
  if (length(ak.hi)>0) { value = value[-ak.hi] }
  
  states.df <- map_data("state")
  states.df = subset(states.df,group!=8) # get rid of DC
  states.df$st <- state.abb[match(states.df$region,tolower(state.name))] # attach state abbreviations
  
  states.df$value = value[states.df$st]
  
  if (!diverge) {
    p = qplot(long, lat, data = states.df, group = group, fill = value, geom = "polygon", xlab="", ylab="", main=main) +
      opts(axis.text.y=theme_blank(), axis.text.x=theme_blank(), axis.ticks = theme_blank(), panel.grid.major = theme_blank(), panel.grid.minor = theme_blank(), panel.background = theme_blank(), panel.border = theme_blank()) +
      scale_fill_continuous (name,low = "#B71B1A", high =  "#3B4FB8", )    
    p2 = p + geom_path(data=states.df, color = "white", alpha = 0.4, fill = NA) + coord_map(project="albers", at0 = 45.5, lat1 = 29.5)    
  } else {

    fill_gradn <- function(pal) {
      scale_fill_gradientn(name, colours = pal(7))
    }
    
      p = qplot(long, lat, data = states.df, group = group, fill = value, geom = "polygon", xlab="", ylab="", main=main) +
        opts(axis.text.y=theme_blank(), axis.text.x=theme_blank(), axis.ticks = theme_blank(), 
             panel.grid.major = theme_blank(), panel.grid.minor = theme_blank(), panel.background = theme_blank(), panel.border = theme_blank())+ 
               fill_gradn(diverge_hcl)

      p2 = p + geom_path(data=states.df, color = "white", alpha = 0.4, fill = NA) + coord_map(project="albers", at0 = 45.5, lat1 = 29.5)     
  }
  
  for (filetype in c(".pdf",".png")) {
    ggsave(file=paste("Plots/maps/",filename,filetype,sep=""), width=8, height=5) 
  }
}

Thanks for any help!

Boris Shor

Jean-Olivier Irisson

unread,
Mar 19, 2012, 4:42:30 PM3/19/12
to Boris Shor, ggp...@googlegroups.com
On 2012-Mar-19, at 19:20 , Boris Shor wrote:
>
> I'm enjoying easy creation of choropleth plots with ggplot 0.8.9 on R2.13. In particular, I'm making use of the diverge color map, drawing from Hadley's examples. I really like the red-to-blue one, but I can't seem to figure out how to flip high and low with it (like I can without a diverge gradient). In the following plot, I'd like the red end to be blue (eg California), and the blue end to be red.

Use
scale_fill_gradient2 with the low, mid, and high and define the midpoint explicitly
or
scale_fill_gradientn with a diverging sequence of colours, if you don't want to pick the midpoint explicitly. colours are used in the order in which they are specified. In you example, we don't know where the pal() function comes from. You can use rev(pal(7)) to reverse the vector of colours or use something like c("red","white","blue") as the colours argument

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/

Boris Shor

unread,
Mar 19, 2012, 7:59:09 PM3/19/12
to ggp...@googlegroups.com
Embarrasingly easy ... thanks! The rev() did the trick. I hadn't used gradient2 before, only gradientn. That should do it, too.
Reply all
Reply to author
Forward
0 new messages