Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

r - How to make gap between x and y axis and protruded ticks in ggplot2

How can I create the following style of graph:

enter image description here

Notice the gap between x-y axis (red circle) and protruded ticks in x-y axis (arrow).

At best I can do is this now:

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

p

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One option is to remove the built-in axis lines and then use geom_segment to add axes with a gap. In order to make it easier to get the broken axis lines in the right place, we also use scale_y_continuous to specify exactly where we want the axis breaks and limits. The code also shows how to increase the size of the tick marks.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...