Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot stat_bin2d plot with heavily skewed data

Tags:

r

ggplot2

I have a set of data that is heavily right skewed. This creates a problem when doing a stat_bin2d plot. The result is most of the graph is dark blue with only a few points are a different color. I'd like to have the graph use the entire color range a bit more.

An example of the problem is from the ggplot documentation direction.

ggplot(diamonds, aes(carat, price)) + stat_bin2d()

The resulting graph has only a few positions that are something other than dark blue.

How can I adjust the mapping of the color range to show more detail? I know I can set the limits, but this doesn't exactly fit the bill as it makes anything outside the limits be gray.

ggplot(diamonds, aes(carat, price)) + stat_bin2d() + scale_fill_gradient(limits=c(1, 100))

Something like this with they gray appropriately colored too.

like image 890
robbie Avatar asked Dec 02 '25 12:12

robbie


1 Answers

The quick answer is

ggplot(diamonds, aes(carat, price)) + stat_bin2d() +
  scale_fill_gradient(trans="log10")

EDIT: A longer answer is that you probably want some kind of transformation of the color or fill scale. For built-in transformations refer to the "See Also" section of

library(scales)
?trans

If none of the built-in transformation is suitable then you can construct your own. See the answers to this SO question about transforming color scales for an example showing how to do this.

like image 148
Ista Avatar answered Dec 04 '25 04:12

Ista



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!