使用ggplot在stat_bin2d上计数和轴标签 [英] Count and axis labels on stat_bin2d with ggplot

查看:245
本文介绍了使用ggplot在stat_bin2d上计数和轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试制作一个二维直方图,其中显示了箱内容和梯度。数据是两个轴中从0到4(仅)的整数。

我尝试使用



是有什么明显的我在颜色渐变和轴标签中做错了?如果有更好的方法可以使用其他包/命令来完成,我也不会与ggplot或stat_bin2d结婚。

解决方案

stat_bin2d 使用 cut 函数来创建垃圾箱。默认情况下, cut 会创建左侧打开并关闭右侧的容器。 stat_bin2d 也设置 include.lowest = TRUE ,这样最低的区间也会在左边关闭。我没有仔细查看 stat_bin2d 的代码,试图弄清楚到底发生了什么问题,但它似乎与在选择 cut 时打破。在任何情况下,您都可以通过将分隔符明确设置为从-1开始来获得所需的行为。例如:

  ggplot(data,aes(x = x,y = y))+ 
geom_bin2d = c(-1:4))+
stat_bin2d(geom =text,aes(label = ..count ..),breaks = c(-1:4))+
scale_fill_gradient( (-1,5)+
ylim(-1,5)+
coord_equal()



要将瓦片置于整数网格点上,将断点设置为半整数值:

  ggplot (数据,aes(x = x,y = y))+ 
geom_bin2d(breaks = seq(-0.5,4.5,1))+
stat_bin2d(geom =text,aes(label = ..count ..),breaks = seq(-0.5,4.5,1))+
scale_fill_gradient(low =snow3,high =red,trans =log10)+
scale_x_continuous (break = 0:4,limits = c(-0.5,4.5))+
scale_y_continuous(break = 0:4,limits = c (-0.5,4.5))+
coord_equal()


I am trying to make a 2D histogram with the individual bins showing both the bin contents and a gradient. The data are integers ranging from 0 to 4 (only) in both axes.

I tried working with this answer but I end up with a few issues. First, a few bins end up getting no gradient at all. In the MWE below, the bottom left bins of 130 and 60 seems to be blank. Second, the bins are shifted to below 0 in both axes. For this axis issue, I found I could simply add a 0.5 to both x and y. In the end though, I also would like to have the axis labels to be centered within a bin and adding that 0.5 does not address that.

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

Is there something obvious I am doing wrong in both the color gradients and axis labels? I am also not married to ggplot or stat_bin2d if there is a better way to do it with some other package/command. Thanks in advance!

解决方案

stat_bin2d uses the cut function to create the bins. By default, cut creates bins that are open on the left and closed on the right. stat_bin2d also sets include.lowest=TRUE so that the lowest interval will be closed on the left also. I haven't looked through the code for stat_bin2d to try and figure out exactly what's going wrong, but it seems like it has to do with how the breaks in cut are being chosen. In any case, you can get the desired behavior by setting the bin breaks explicitly to start at -1. For example:

ggplot(data, aes(x = x, y = y)) +
  geom_bin2d(breaks=c(-1:4)) + 
  stat_bin2d(geom = "text", aes(label = ..count..), breaks=c(-1:4)) + 
  scale_fill_gradient(low = "snow3", high = "red", trans = "log10") + 
  xlim(-1, 5) +
  ylim(-1, 5) +
  coord_equal() 

To center the tiles on the integer lattice points, set the breaks to half-integer values:

ggplot(data, aes(x = x, y = y)) +
  geom_bin2d(breaks=seq(-0.5,4.5,1)) + 
  stat_bin2d(geom = "text", aes(label = ..count..), breaks=seq(-0.5,4.5,1)) + 
  scale_fill_gradient(low = "snow3", high = "red", trans = "log10") + 
  scale_x_continuous(breaks=0:4, limits=c(-0.5,4.5)) +
  scale_y_continuous(breaks=0:4, limits=c(-0.5,4.5)) +
  coord_equal()

Or, to emphasize that the values are discrete, set the bins to be half a unit wide:

ggplot(data, aes(x = x, y = y)) +
  geom_bin2d(breaks=seq(-0.25,4.25,0.5)) + 
  stat_bin2d(geom = "text", aes(label = ..count..), breaks=seq(-0.25,4.25,0.5)) + 
  scale_fill_gradient(low = "snow3", high = "red", trans = "log10") + 
  scale_x_continuous(breaks=0:4, limits=c(-0.25,4.25)) +
  scale_y_continuous(breaks=0:4, limits=c(-0.25,4.25)) +
  coord_equal()

这篇关于使用ggplot在stat_bin2d上计数和轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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