开发者

Setting breakpoints for data with scale_fill_brewer() function in ggplot2

I am creating a map (choropleth) as described on the ggplot2 wiki. Everything works like a charm, except that I am running into an issue mapping a continuous value to the polygon fill color via the scale_fill_brewer() function.

This question describes the problem I'm having. As in the answer, my workaround has been to pre-cut my data into bins using the gtools quantcut() function:

UPDATE: This first example is actually the right way to do this

require(gtools) # needed for quantcut()

...

fill_factor <- 开发者_高级运维quantcut(fill_continuous, q=seq(0,1,by=0.25))
ggplot(mydata) + 
aes(long,lat,group=group,fill=fill_factor) +
geom_polygon() +
scale_fill_brewer(name="mybins", palette="PuOr")

This works, however, I feel like I should be able to skip the step of pre-cutting my data and do something like this with the breaks option:

ggplot(mydata) +
aes(long,lat,group=group,fill=fill_continuous) +
geom_polygon() +
scale_fill_brewer(names="mybins", palette="PuOr", breaks=quantile(fill_continuous))

But this doesn't work. Instead I get an error something like:

Continuous variable (composite score) supplied to discrete scale_brewer.

Have I misunderstood the purpose of the "breaks" option? Or is breaks broken?


A major issue with pre-cutting continuous data is that there are three pieces of information used at different points in the code:

  • The Brewer palette -- determines the maximum number of colors available
  • The number of break points (or the bin width) -- has to be specified with the data
  • The actual data to be plotted -- influences the choice of the Brewer palette (sequential/diverging)

A true vicious circle. This can be broken by providing a function that accepts the data and the palette, automatically derives the number of break points and returns an object that can be added to the ggplot object. Something along the following lines:

fill_brewer <- function(fill, palette) {
  require(RColorBrewer)
  n <- brewer.pal.info$maxcolors[palette == rownames(brewer.pal.info)]
  discrete.fill <- call("quantcut", match.call()$fill, q=seq(0, 1, length.out=n))
  list(
    do.call(aes, list(fill=discrete.fill)),
    scale_fill_brewer(palette=palette)
  )
}

Use it like this:

ggplot(mydata) + aes(long,lat,group=group) + geom_polygon() +
  fill_brewer(fill=fill_continuous, palette="PuOr")


As Hadley explains, the breaks option moves the ticks, but does not make the data continuous. Therefore pre-cutting the data as per the first example in the question is the right way to use the scale_fill_brewer command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜