Align the pincer axes with the bins in the grid bar graph

I plotted a histogram using a 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 boxes I receive do not match the exact hours, while I would like the box to start at a specific hour, for example 6.7, etc. I am using a grid as I want conditional histograms. I have included only one histogram here for illustration. enter image description here

UPDATE: Here is a reproducible example (hopefully) as requested. As you can see, 0, for example, is not at the limit of bins.

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

      

enter image description here

+3


source to share


1 answer


This is because you specified the x-axis scale with scales = list(x = list(at = 5:15))

, but you did not actually change the breakpoints. This also happens in the default case: the axis labels are integers by default, but the default breakpoints are programmatically defined and not necessarily integers if you don't have integer data.

An easy solution would be to specify your own breaks in the argument 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")

      



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

      

+2


source







All Articles