0

I am creating a barplot with some values by using R and ggplot2. Creating the plot and everything works nicely, however, I have y-values that are mainly in one general value range with one or two vastly higher values mixed in.

To clarify, my y-values look similar to this:

[10, 12, 13, 5000, 14, 11]

y = c(10, 12, 13, 5000, 14, 11)

(For the sake of completeness: the x-values are simple increments of time, e.g. 1,2,3,4,5)

Creating the plot and everything works nicely, except that due the vastly higher value, the whole chart is very "zoomed out". I know I can filter out these higher values by setting limits, however I would like to keep them in the plot and hence wonder if there is any way to set a visible "break" in the y-scale so that it proceeds at a value that is in the range of the higher value.

I have a created a little picture using paint to clarify what I mean:

image

Is there any way to do this using R/ggplots2? Alternatively, I'd also like to know if anybody has any alternative ideas of how these kind of values can be nicely plotted without having to note them separately.

Thanks,

Klinring

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Klinring
  • 3
  • 1

1 Answers1

0

The problem can be solved by creating a new binary variable depending on the value of y. In order to do that, a threshold must be established beforehand.

library(ggplot2)

threshold <- 25
df1$big <- factor(df1$y <= threshold, labels = c("> 25", "<= 25"))

ggplot(df1, aes(x, y)) +
  geom_col() +
  facet_wrap(~ big, "free_y", nrow = 2)

enter image description here

Then the new variable can be removed.

df1$big <- NULL

Data.

df1 <- data.frame(x = LETTERS[1:6], 
                  y = c(10, 12, 13, 5000, 14, 11))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66