在格子直方图中对齐轴与桶的刻度。

时间:2022-05-05 14:57:27

I plotted a histogram using 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("")))`

The bins I get do not match the exact hours, whereas I would like the bin to start at the exact hour, for example, 6,7 etc. I use lattice since I want conditional histograms. I have extracted here just one histogram to illustrate. 在格子直方图中对齐轴与桶的刻度。

我得到的回收箱不匹配准确的时间,而我希望垃圾箱从准确的时间开始,例如,6,7等等。我使用晶格,因为我想要有条件直方图。我在这里提取了一个直方图来说明。

UPDATE: Here is a reproducible example (I hope) as was requested. As can be seen 0 for example is not at the limit of bins.

更新:这里有一个可复制的示例(希望如此)。可以看到,例如0不在容器的极限。

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

在格子直方图中对齐轴与桶的刻度。

1 个解决方案

#1


2  

This happens because you specified the x axis scale with scales = list(x = list(at = 5:15)), but you didn't actually change the breakpoints. It happens in the default case as well: the default axis labels are integers, but the default breakpoints are determined programmatically and are not necessarily integers unless you have integer-valued data.

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

An easy fix would be to specify your own breaks in the breaks argument:

一个简单的解决办法是在break参数中指定您自己的break:

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

And an example:

一个例子:

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

#1


2  

This happens because you specified the x axis scale with scales = list(x = list(at = 5:15)), but you didn't actually change the breakpoints. It happens in the default case as well: the default axis labels are integers, but the default breakpoints are determined programmatically and are not necessarily integers unless you have integer-valued data.

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

An easy fix would be to specify your own breaks in the breaks argument:

一个简单的解决办法是在break参数中指定您自己的break:

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

And an example:

一个例子:

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