r - 将轴刻度与点阵直方图中的箱对齐

标签 r plot lattice

我使用格子绘制直方图

histogram(~Time |factor(Bila), data=flexi2, xlim= c(5, 15), ylim=c(0, 57),
      scales=list(x=list(at=seq(5,15,1))), xlab="Time", 
      subset=(Bila%in% c("")))`

我得到的箱子与确切的时间不匹配,而我希望箱子在确切的时间开始,例如 6,7 等。我使用 lattice 因为我想要条件直方图。我在这里只提取了一个直方图来说明。 enter image description here

更新: 这是一个可重现的例子(我希望如此),正如所要求的那样。可以看出,例如 0 不在 bin 的限制内。

x<-rnorm(1000)
histogram(~x)

enter image description here

最佳答案

发生这种情况是因为您使用 scales = list(x = list(at = 5:15)) 指定了 x 轴刻度,但您实际上并没有更改断点。它也发生在默认情况下:默认轴标签是整数,但默认断点是通过编程确定的,不一定是整数,除非您有整数值数据。

一个简单的解决方法是在 breaks 参数中指定您自己的中断:

histogram(~Time |factor(Bila), data=flexi2, subset=(Bila %in% c("")),
  xlim= c(5, 15), ylim=c(0, 57),
  breaks = 5:15,
  scales = list(x = list(at = 5:15)),
  xlab="Time")

还有一个例子:

library(lattice)
x <- rnorm(1000)
x[abs(x) > 3] <- 3
x_breaks <- c(-3, -1.5, 0, 1.5, 3)
histogram(~ x,
          title = "Defaults")
histogram(~ x, breaks = x_breaks,
          title = "Custom bins, default tickmarks")
histogram(~ x, scales = list(x = list(at = x_breaks)),
          title = "Custom tickmarks, default bins")
histogram(~ x, breaks = x_breaks, scales = list(x = list(at = x_breaks)),
          title = "Custom tickmarks, custom bins")

关于r - 将轴刻度与点阵直方图中的箱对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942798/

相关文章:

r - 在 R 中使用 ggplot2 在 bin 的边缘(而不是中心)有刻度吗?

r - 将段值从 Power BI 传递到其数据源 脚本 R

python - 有没有适用于 Python 3.0 的图形/绘图/类似库?

r - 在ggplot中绘制置信区间

r - Uniroot log(x) 解决方案

regex - 在 R 中组合字符向量中的行

algorithm - 比较绘图的最快方法

r - 更改点阵图中 strip 上的文本

r - R中的哈维球

r - R 中最小的 2x4 格子条形图?