Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

r - gradient breaks in a ggplot stat_bin2d plot

I have a 2d histogram created with stat_bin2d in the ggplot2 package. I'd like to control both the number of breaks in the color gradient, as well as where those breaks are located. I'm sure I'm just overlooking something small, but I can't figure out how to control the breaks in the binning.

Example:

x <- rnorm(100)^2
y <- rnorm(100)^2
df <- data.frame(x,y)
require(ggplot2)
p <- ggplot(df, aes(x, y)) 
p <- p + stat_bin2d(bins = 20)
p + scale_colour_gradient2(breaks=c(1,2,3,4,5,6))

This produces:

enter image description here

This plot only has 3 breaks at c(5,10,15) despite my futile attempt to put breaks at c(1,2,3,4,5,6))

Any hints?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

here is an example combining cut and bin2d:

p <- ggplot(df, aes(x, y, fill=cut(..count.., c(0,6,8,9,Inf))))
p <- p + stat_bin2d(bins = 20)
p + scale_fill_hue("count")

As there are many ways to make the breaks arbitrary, if you define clearly what you want, probably you can get a better answer.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...