Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change colour scheme for ggplot geom_polygon in R

I'm creating a map using the maps library and ggplot's geom_polygon. I'd simply like to change the default blue, red, purple colour scheme to something else. I'm extremely new to ggplot so please forgive if I'm just not using the right data types. Here's what the data I'm using looks like:

> head(m)
region      long      lat group order subregion Group.1 debt.to.income.ratio.mean    ratio total
17 alabama -87.46201 30.38968     1     1      <NA> alabama                   12.4059   20.51282    39
18 alabama -87.48493 30.37249     1     2      <NA> alabama                   12.4059 20.51282    39
19 alabama -87.52503 30.37249     1     3      <NA> alabama                   12.4059 20.51282    39
20 alabama -87.53076 30.33239     1     4      <NA> alabama                   12.4059 20.51282    39
21 alabama -87.57087 30.32665     1     5      <NA> alabama                   12.4059 20.51282    39
22 alabama -87.58806 30.32665     1     6      <NA> alabama                   12.4059 20.51282    39

> head(v)
          Group.1 debt.to.income.ratio.mean    ratio     region total
alabama       alabama                  12.40590 20.51282    alabama    39
alaska         alaska                  11.05333 33.33333     alaska     6
arizona       arizona                  11.62867 25.55556    arizona    90
arkansas     arkansas                  11.90300  5.00000   arkansas    20
california california                  11.00183 32.59587 california   678
colorado     colorado                  11.55424 30.43478   colorado    92

Here's the code:

library(ggplot2)
library(maps)

states <- map_data("state")
m <- merge(states, v, by="region")
m <- m[order(m$order),]

p<-qplot(long, lat, data=m, group=group, fill=ratio, geom="polygon")

I've tried the below and more:

cols <- c("8" = "red","4" = "blue","6" = "darkgreen", "10" = "orange") 
p + scale_colour_manual(values = cols)
p + scale_colour_brewer(palette="Set1")
p + scale_color_manual(values=c("#CC6666", "#9999CC"))
like image 369
tcash21 Avatar asked May 13 '11 18:05

tcash21


People also ask

How do I specify colors in ggplot2?

A color can be specified either by name (e.g.: “red”) or by hexadecimal code (e.g. : “#FF1234”).

How do I change colors in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.


1 Answers

The problem is that you are using a color scale but are using the fill aesthetic in the plot. You can use scale_fill_gradient() for two colors and scale_fill_gradient2() for three colors:

p + scale_fill_gradient(low = "pink", high = "green") #UGLY COLORS!!!

I was getting issues with scale_fill_brewer() complaining about a continuous variable supplied when a discrete variable was expected. One easy fix is to create discrete bins with cut() and then use that as the fill aesthetic:

m$breaks <- cut(m$ratio, 5) #Change to number of bins you want

p <- qplot(long, lat, data = m, group = group, fill = breaks, geom = "polygon")
p + scale_fill_brewer(palette = "Blues")
like image 134
Chase Avatar answered Oct 07 '22 10:10

Chase