How to make gap between x / y axis and protruding tongs in ggplots

How to create the following graph style:

enter image description here

Note the gap between the xy-axis (red circle) and the protruding pliers along the xy-axis (arrow).

At best I can do this now:

library(ggplot2)
p <- ggplot(mpg, aes(class, hwy)) +
     geom_boxplot() +
     theme_bw(base_size=10) 

p

      

enter image description here

+5


source to share


3 answers


You can achieve something similar using ggthemes

which provides geom_rangeframe

and theme_tufte

.

library(ggplot2)
library(ggthemes)
ggplot(mpg, aes(class, hwy)) + 
  geom_boxplot() + 
  geom_rangeframe() + 
  theme_tufte() +
  theme(axis.ticks.length = unit(7, "pt"))

      



enter image description here More inspiration here .

+6


source


One option is to remove inline axes and then use geom_segment

to add axes with a space. To make it easier to get the broken centerlines in the right place, we also use scale_y_continuous

to pinpoint where we want the axial breaks and limits. The code also shows how to increase the size of the labels.

ggplot(data=mpg, aes(class, hwy)) +
  geom_segment(y=10, yend=50, x=0.4, xend=0.4, lwd=0.5, colour="grey30", lineend="square") +
  geom_segment(y=5, yend=5, x=1, xend=length(unique(mpg$class)), 
               lwd=0.5, colour="grey30", lineend="square") +
  geom_boxplot() +
  scale_y_continuous(breaks=seq(10,50,10), limits=c(5,50), expand=c(0,0)) +
  theme_classic(base_size=12) +
  theme(axis.line = element_blank(),
        axis.ticks.length = unit(7,"pt")) 

      



enter image description here

+5


source


The lines at the top and bottom of the lines are added with

geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)

      

or by adding geometry to another layer.

stat_summary(fun.data = mean_sdl,
             fun.args = list(mult = 1),
             geom = "errorbar",
             width = 0.1)

      

-1


source







All Articles