【发布时间】:2016-12-07 02:57:09
【问题描述】:
我正在尝试制作一个 2D 直方图,其中包含显示 bin 内容和梯度的各个 bin。数据是两个轴上从 0 到 4(仅)的整数。
我尝试使用this answer,但最终遇到了一些问题。首先,一些箱子最终根本没有梯度。在下面的 MWE 中,左下角的 130 和 60 似乎是空白的。其次,bin 在两个轴上都移动到 0 以下。对于这个轴问题,我发现我可以简单地在 x 和 y 上添加 0.5。最后,我还希望轴标签在 bin 内居中,并添加 0.5 并不能解决这个问题。
library(ggplot2)
# Construct the data to be plotted
x <- c(rep(0,190),rep(1,50),rep(2,10),rep(3,40))
y <- c(rep(0,130),rep(1,80),rep(2,30),rep(3,10),rep(4,40))
data <- data.frame(x,y)
# Taken from the example
ggplot(data, aes(x = x, y = y)) +
geom_bin2d(binwidth=1) +
stat_bin2d(geom = "text", aes(label = ..count..), binwidth=1) +
scale_fill_gradient(low = "snow3", high = "red", trans = "log10") +
xlim(-1, 5) +
ylim(-1, 5) +
coord_equal()
我在颜色渐变和轴标签中是否有明显的错误?如果有更好的方法来使用其他包/命令,我也没有与 ggplot 或 stat_bin2d 结婚。提前致谢!
【问题讨论】: