Rui
Rui

Reputation: 1

How to produce bins "greather than" in ggplot2 histogram

I want to make a histogram of counts of some individuals but divided by 11 lenght classes. The classes are: ]50,100 mm[; ]100-150 mm]; ]150-200 mm]; ]200-250 mm]; ]250-300 mm]; ]300-350 mm]; ]350-400 mm]; ]400-450 mm]; ]450-500 mm]; ]500-550 mm]; > 550 mm. My main problem is, how can I group the individuals that are greather than 550mm in one bin?

Currently my script is:

ggplot(LWR, aes(x=TL, fill=Period)) +
geom_histogram( color="#e9ecef", alpha=0.6, position = 'dodge',boundary= 50, binwidth=50,closed="left") +
scale_fill_manual(values=c("#69b3a2", "#404080")) +
scale_y_continuous(name="Number of eels", breaks=seq(0, 400, by=50),
limits = c(0, 400),expand=expansion(mult=c(0,0.05))) +
scale_x_continuous(name="Total length (mm)",breaks=seq(50, 550, by=50),
limits = c(50, 550))

and I get this histogram

Did the values greather than 550mm disappeared or are in the last bin?

Upvotes: 0

Views: 77

Answers (1)

stefan
stefan

Reputation: 125133

One option would be to set the set the out-of-bounds behavior to oob=squish so that obs. outside of the limits are "squished" into the last bin whereas the default oob=censor will drop them or set them to NA.

Using some fake random example data:

library(ggplot2)

set.seed(123)

LWR <- data.frame(
  TL = exp(rnorm(1000, 5, 1)),
  Period = sample(c("Pre", "Pos"), 1000, replace = TRUE, prob = c(.6, .4))
)

base <- ggplot(LWR, aes(x = TL, fill = Period)) +
  geom_histogram(color = "#e9ecef", alpha = 0.6, position = "dodge", boundary = 50, binwidth = 50, closed = "left") +
  scale_fill_manual(values = c("#69b3a2", "#404080")) +
  scale_y_continuous(name = "Number of eels")

p1 <- base +
  scale_x_continuous(
    name = "Total length (mm)", breaks = seq(50, 550, by = 50),
    limits = c(50, 550)
  ) +
  labs(title = "Default oob = censor")

p2 <- base +
  scale_x_continuous(
    name = "Total length (mm)", breaks = seq(50, 550, by = 50),
    limits = c(50, 550), oob = scales::squish
  ) +
  labs(title = "Using oob = squish")

library(patchwork)

p1 + p2
#> Warning: Removed 234 rows containing non-finite values (`stat_bin()`).

Upvotes: 1

Related Questions