A theme for axis for ggplot2 - gap between x and y axis

Viewed 114

I used the following code to generate a simple histogram:

d<-rnorm(1000, o, 1);
hist(d)

enter image description here

We can create similar axes setting in the base R plot command by using bty="n".

My question: Is there any way in ggplot to create such an axes setting (the axes are not intersected at the origin)?

Farrokh

1 Answers

The ggh4x package has truncated axes (Disclaimer: I wrote the package, so I'm not unbiased). By default, it cuts the axis off at the two most extreme breakpoints, but you can set different options.

library(ggplot2)
library(ggh4x)

set.seed(42)
df <- data.frame(d = rnorm(1000))

ggplot(df, aes(d)) +
  geom_histogram() +
  guides(x = "axis_truncated", y = "axis_truncated") +
  theme(axis.line = element_line())
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2021-04-19 by the reprex package (v1.0.0)

Related