log scale for geom_point attributes

3,368 views
Skip to first unread message

Matt

unread,
Mar 1, 2011, 2:43:00 PM3/1/11
to ggplot2
Hi,

For a scatterplot, I would like to defined the point characters based
on their log transformed value. The following command produces the
plot I want, but the legend contains the exponents (1,2,3 ..) and I
would like the values printed in the origninal units. ( 10, 100,
1,000, .... )

p + geom_point( aes( color = log10(value), size = log10(value) ) )


Any suggestions?

Thanks,

Matt

Scott Chamberlain

unread,
Mar 1, 2011, 3:09:37 PM3/1/11
to Matt, ggplot2
p <- ggplot(mtcars, aes(wt, mpg)) 
p + geom_point( aes( color = mpg, size = mpg ) ) +
scale_y_log10()
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brian Diggs

unread,
Mar 1, 2011, 7:38:27 PM3/1/11
to ggplot2
ggplot(diamonds) +
geom_point(aes(x=depth, y=table, size=price)) +
scale_size(trans="log10")

ggplot(diamonds) +
geom_point(aes(x=depth, y=table, size=price)) +
scale_size(trans="log10", breaks=c(1000,3000,10000),
labels=c(1000,3000,10000))

Size scales can be transformed like other continuous scales.

--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University


> Thanks,
>
> Matt

Matt Pocernich

unread,
Mar 2, 2011, 10:37:23 AM3/2/11
to Scott Chamberlain, ggplot2
Thanks for your reply Scott.

Using your example, what I a trying to do is get the plotting
characters for the variable described in the legend expressed in a
log scale in the original units.

This would be the appearance of the plot I want, except I would like
the legend to be in the original units of engine displacement.

p <- ggplot(mtcars, aes(wt, mpg))

p + geom_point( aes( color = log10(disp), size = log10(disp ) ) )

Sorry of the confusion.

Matt

Scott Chamberlain

unread,
Mar 2, 2011, 11:05:27 AM3/2/11
to Matt Pocernich, ggplot2
You can do one of these two:

ggplot(mtcars, aes(wt, mpg)) +

geom_point(aes(color = disp, size = disp)) +

scale_colour_gradient(trans="log10")


ggplot(mtcars, aes(wt, mpg)) +

geom_point(aes(color = disp, size = disp)) +

scale_size(trans="log10")


Or add in a line to take out one of the two legends, either the one for size or colour:


scale_size(legend=F)

scale_colour_gradient(legend=F)

Matt Pocernich

unread,
Mar 2, 2011, 11:11:50 AM3/2/11
to Scott Chamberlain, ggplot2
Excellent!

Is there a way to combine color and size attributes into the same legend?

Thanks,

Matt

Jens

unread,
Mar 4, 2011, 5:36:32 AM3/4/11
to ggplot2
Hi Matt,

sure, as mentioned by Hadley in another discussion, you just need to
set the breaks manually, like this:

ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(color = disp, size = disp)) +
scale_colour_gradient(trans="log10") +
scale_size(breaks = c(100,200,300,400)) +
scale_colour_gradientn(breaks = c(100,200,300,400), colours =
heat.colors(9))

(or use pretty())

Regards, Jens
Reply all
Reply to author
Forward
0 new messages