2

I need to produce a set of histograms where there is a vertical line, in some cases far away from the values of the histogram, sort of like this:

hist(mtcars$mpg, breaks = 15, xlim=c(0,160))
abline(v=150, lwd=2, lty=2, col="blue")

In base R if I extend the x axis and add the vertical line, the histogram itself does not change.

Ideally I would do the plots with ggplot instead of base R, but if I do this, on adding the vertical line the histogram itself changes:

ggplot(data = mtcars, aes(x=mpg))+
  geom_vline(xintercept = 150, color= "blue", linetype="dashed")+
  scale_x_continuous(c(0,160))+
  geom_histogram(binwidth = 15, color="black", fill="white")

(First histogram is in base R, second with ggplot)

enter image description here

How can I produce the same histogram as in base R, with the vertical line, in ggplot?

Reader 123
  • 285
  • 1
  • 7
  • As per my long "answer-comment" - adding vline doesn't really change ggplot's histogram. Maybe you want ggplot to look like base R: In this case, your question would be duplicate to: https://stackoverflow.com/questions/25146544/r-emulate-the-default-behavior-of-hist-with-ggplot2-for-bin-width – tjebo Apr 22 '23 at 19:48

2 Answers2

1

Here is the exact same plot with ggplot2. The main task was to set the correct binwidth and breaks:

library(ggplot2)

ggplot(mtcars, aes(x = mpg)) +
  geom_histogram(binwidth = 2, color = "black", fill = "grey80", breaks = seq(0, 160, by = 2)) +
  geom_vline(xintercept = 150, linetype = "dashed", color = "blue", size = 1) +
  scale_x_continuous(limits = c(-10, 160), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0, 7.5), expand = c(0, 0)) +
  theme_classic()

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
0

But does it really change?

library(ggplot2)
library(patchwork)

p1 <- ggplot(data = mtcars, aes(x=mpg))+
  scale_x_continuous(c(0,160))+
  geom_histogram(binwidth = 15, color="black", fill="white") +
  ggtitle("just the histogram")
  
p2 <- ggplot(data = mtcars, aes(x=mpg))+
  scale_x_continuous(c(0,160))+
  geom_histogram(binwidth = 15, color="black", fill="white") +
  geom_vline(xintercept = 150, color= "blue", linetype="dashed") +
  ggtitle("with vline")

p1 + p2

Created on 2023-04-22 with reprex v2.0.2

I'm not quite sure what you expect "not to change". Maybe you want ggplot to look like base R: In this case, your question would be duplicate to: R - emulate the default behavior of hist() with ggplot2 for bin width

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Tjebo, apologies, maybe I did not explain myself well enough: if you run the ggplot code without adding the vertical line, the histogram will look like the one in base R. When you add the vertical line, base R behaves as expected, i.e. it adds the line and nothing else changes. Ggplot, on the other hand, changes the whole histogram when you add the vertical line. – Reader 123 Apr 22 '23 at 21:06
  • 1
    @Reader123 no! tgisnis exactly what I demonstrated in this answer that ggplot2 does not change the histogram when adding a vline layer. – tjebo Apr 24 '23 at 02:14
  • 1
    Thanks, @Tjebo. I now see what you mean. I will stick with Tarjae's answer because it gave me exactly what I needed, but I appreciate you taking the time to respond. – Reader 123 Apr 25 '23 at 08:37