梯度中断ggplot stat_bin2d图 [英] gradient breaks in a ggplot stat_bin2d plot

查看:168
本文介绍了梯度中断ggplot stat_bin2d图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ggplot2 包中使用 stat_bin2d 创建了一个2d直方图。我想要控制颜色渐变中的中断数量以及这些中断的位置。我敢肯定,我只是忽略了一些小事,但我无法弄清楚如何控制分档中的休息。例如:

  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))

这产生:


这个图只有 c(5,10,15)尽管我徒劳地试图在 c(1,2,3,4,5,6)处放置休息符)



任何提示?

解决方案

code> cut 和 bin2d

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

如果您定义了cle,许多方法可以使这些断点变得随意arly你想要什么,可能你会得到一个更好的答案。


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:

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?

解决方案

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.

这篇关于梯度中断ggplot stat_bin2d图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆