如何在ggplots中使x/y轴和突出的刻度之间产生间隙

nev*_*int 5 r ggplot2

如何创建以下样式的图形:

在此输入图像描述

注意xy轴(红色圆圈)和xy轴(箭头)上的突出刻度之间的间隙.

我现在最好能做的就是:

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

p
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

nei*_*fws 6

你可以实现类似的东西ggthemes提供geom_rangeframetheme_tufte.

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

在此输入图像描述 这里有更多灵感.


eip*_*i10 5

一种选择是移除内置轴线,然后用于geom_segment添加带间隙的轴.为了更容易将断开的轴线放在正确的位置,我们还使用scale_y_continuous指定轴断裂和限制的确切位置.该代码还显示了如何增加刻度线的大小.

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")) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述