Home > Net >  show a specific value at x axis without break in ggplot2
show a specific value at x axis without break in ggplot2

Time:09-27

Context

I want to show a specific value at x axis in ggplot2. It is 2.84 in the Reproducible code.

I found the answer at enter image description here

CodePudding user response:

You can automate this process by creating a wrapper round scale_x_continuous that inserts your break into a vector of pretty breaks:

scale_x_fancy <- function(xval, ...) {
  scale_x_continuous(breaks = ~ sort(c(pretty(.x, 10), xval)), ...)
}

So now you just add the x value(s) where you want the extra break to appear:

ggplot(d, aes(x, y))   
  geom_point()   
  theme_minimal()  
  scale_x_fancy(xval = 2.84, name = "Number of treatments")

enter image description here

  • Related