1

I am trying to make a histogram with two groups: one where (x<60 || x>100), and one where that condition is false.

Basically like this: histogram with vertical bars and colored groups

update 2023.08.03 - - - - - - -

This code is exactly what I want, but the color seems to be leaking between the lines:

histogram(~pulse, data = data, v = c(60,100), breaks = c(seq(from=40,to=140,by=20)), type = "percent", groups = ((pulse >= 100) | (pulse <= 60)))

histogram with vertical lines at 60 and 100, orange outside the lines and blue inside with some orange in the blue bars image

I tried:

histogram(~pulse, data = data, v = 100,
    breaks = c(seq(from=40,to=140,by=20)),
    type = "percent", groups = (pulse >= 100))
histogram(~pulse, data = data, v = num,
    breaks = c(40,60,80,num,120,140),
    type = "percent", groups = (pulse >= 100))
histogram(~pulse, data = data, v = 100,
    breaks = c(seq(from=40,to=140,by=10)),
    type = "percent", groups = (pulse >= 100))
histogram(~pulse, data = data, v = 100,
    type = "percent", groups = (pulse >= 100))

If you have any ideas please let me know! I'm really hoping to stick to Lattice since I already have quite a few of these typed out ':)

end of update - - - - - - -

   

I tried this code but it gives me an error:

histogram(~pulse, data=data, type="percent", breaks=10, labels = TRUE, groups = (pulse > 60))
  • Error using packet 1
  • 'x' and 'units' must have length > 0

 

This code works, but it is only half of what I want:

set.seed(123)
var <- rnorm(700, 0, 0.25)
df <- as.data.frame(var)
num <- 0.5
histogram(~var, data = df, v = num, width = 0.2, type = "percent", groups = (var >= num))

My output: histogram with vertical line that splits it into blue and orange image

 

I have vague ideas of messing with panel or panel.superpose or maybe allow.multiple?

 

If this is relevant, here is my session info and more on the first histogram:

R version 4.3.0 (2023-04-21 ucrt) -- "Already Tomorrow"
Platform: x86_64-w64-mingw32/x64 (64-bit)

histogram variables image

  • 3
    I think you might be committing a logical error. The expression `(x>60 || x<100),` is going to be true for every numeric value. Perhaps you want `&`? I'm also guess ing that you have not provided an accurate recounting of the error message: It's hard to believe it really said "'x' and 'units' must have length < 0" – IRTFM Jul 12 '23 at 02:12
  • Welcome to stack overflow! So, is it that you want one histogram showing the tails of the distribution, and one showing the center? @IRTFM is correct in saying that your logical statement `(x>60 || X<100)` is true for all values, but if you want the tails and the center on separate plots, I'm sure we can help. – Reed Merrill Jul 12 '23 at 03:39
  • Hi Isabelle, welcome to StackOverflow! Re: the code below where you said "This code works, but it is only half of what I want:", doesn't actually change colour for me ...? It does produce a histogram, but one with only one colour, blue – Mark Jul 12 '23 at 05:11
  • @Mark I added an image of what the working histogram looks like for me – isabelle-smith Jul 12 '23 at 21:31
  • @IRTFM I fixed the signs for both the expression and the error message (I just had them backwards) – isabelle-smith Jul 12 '23 at 21:33
  • @ReedMerrill do they need to be on separate plots? I definitely want it to look like just one histogram, but I am not that familiar with lattice – isabelle-smith Jul 12 '23 at 21:47
  • @isabelle-smith ah, putting it all on a single plot actually makes more sense and is probably easier to do. I think this would be easier using `ggplot`. I'll write an answer tonight. – Reed Merrill Jul 12 '23 at 22:50
  • 1
    Seems to me that this answer probably has all the code to do this in the Lattice system: https://stackoverflow.com/questions/33850491/how-to-superimpose-a-histogram-on-each-panel/33904407#33904407. Basically you just need to create one variable that holds the class of the outliers and only plot one panel. – IRTFM Jul 12 '23 at 23:44
  • @IRTFM making two separate histograms does not work when the graph is in percent/proportions (P.S. if you are still interested I updated the post after some more messing around) – isabelle-smith Aug 03 '23 at 19:27

0 Answers0