set_default_scale

1,707 views
Skip to first unread message

baptiste auguie

unread,
Feb 7, 2012, 2:05:34 PM2/7/12
to ggplot2-dev
Hi,

I cannot find an equivalent for set_default_scale() in v-0.9.0. There
is update_geom_defaults(geom, new) and update_stat_defaults(stat,
new), but what about scales?

The purpose would be to make it easier to switch between black and
white and colour figures in a document by adding a single line at the
top,

set_default_scale("colour", "discrete", "grey")

or something like that.

Thanks,

baptiste

Hadley Wickham

unread,
Feb 7, 2012, 4:30:48 PM2/7/12
to baptiste auguie, ggplot2-dev
Hi Baptiste,

You should be able to do:

scale_colour_discrete <- scale_grey

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

baptiste auguie

unread,
Feb 7, 2012, 5:56:54 PM2/7/12
to Hadley Wickham, ggplot2-dev
Thanks, that works.

More generally though, can I set the default colours used by, say,
scale_fill_continuous? I'd like it to use shades of grey, and there is
no continuous scale_fill_grey as far as I can tell.

The example below is from the transition guide, where one might want
to switch between colour and BW for the whole document.

df2 <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
(p1 <- ggplot(df2, aes(X1, X2)) + geom_tile(aes(fill = value)) +
geom_point(aes(size = value)))

Ideally I could set the default colours in a chunk at the beginning of
the document, but I don't see a clean way of doing it. Something along
those lines, maybe?

update_scale_defaults <- function(scale, ...){
fun <- get(scale)
f <- formals(fun)
f2 <- modifyList(f, list(...))
formals(fun) <- f2
fun
}

<<>>=
bw_version <- TRUE
@

<<colour, eval=!bw_version>>
theme_set(theme_grey())
update_scale_defaults(scale_colour_continuous, low = "blue", high =
"red", na.value="grey50")
@

<<bw, eval=bw_version>>
theme_set(theme_bw())
scale_fill_discrete <- scale_fill_grey
scale_color_discrete <- scale_color_grey
update_scale_defaults(scale_colour_continuous, low = "white", high =
"black", na.value="#000000FF")
@


Cheers,

b.

Hadley Wickham

unread,
Feb 7, 2012, 6:00:24 PM2/7/12
to baptiste auguie, ggplot2-dev
I think you're trying too hard:

scale_colour_continuous <- function(...) scale_colour_gradient(low =
"blue", high =
"red", na.value="grey50", ...)

?

Hadley


On Tue, Feb 7, 2012 at 4:56 PM, baptiste auguie

baptiste auguie

unread,
Feb 7, 2012, 6:19:13 PM2/7/12
to Hadley Wickham, ggplot2-dev
Good point!

So I guess the more comprehensive black and white switch might look like,

if(black_and_white) {

theme_set(theme_bw())

scale_fill_discrete <- scale_fill_grey
scale_color_discrete <- scale_color_grey

scale_colour_continuous <- function(...)
scale_colour_gradient(low ="white", high ="black",
na.value="#000000FF", ...)
scale_fill_continuous <- function(...)
scale_fill_gradient(low ="white", high ="black", na.value="#000000FF", ...)

my_colours <- scales::grey_pal(start = 0.2, end = 0.8)
## to be used in e.g. scale_colour_gradientn(colors=my_colours)
}


Thanks,

b.

Reply all
Reply to author
Forward
0 new messages