bty = “n” in ggplot2

2019-06-27 20:51发布

问题:

Is there a way in ggplot2 to make the not being together axis such as bty="n" in normal R graphics?

Like this:

Thank you

回答1:

theme_classic produces something pretty similar to the above

ggplot(faithful, aes(x=eruptions, y=waiting)) + 
  geom_point(shape=21) +
  theme_classic()



回答2:

It's a little bit clunky, but you can do it by suppressing the axes and annotating with segments in the appropriate places: it's useful to know that ggplot will place elements with x/y coordinates of -Inf at the left/bottom of the plot ...

 library("ggplot2")
 axrange <- list(y=c(50,90),x=c(2,5))
 g0 <- ggplot(faithful, aes(x=eruptions, y=waiting)) + 
   geom_point(shape=21)
 g0 +
   theme_classic()+
   theme(axis.line.y=element_blank(),axis.line.x=element_blank())+
   annotate("segment",x=-Inf,xend=-Inf,y=axrange$y[1],yend=axrange$y[2])+
   annotate("segment",y=-Inf,yend=-Inf,x=axrange$x[1],xend=axrange$x[2])

I don't know of an easier/more automatic way; I don't think one exists, but hopefully I'm wrong.

The Tufte theme from the ggthemes package gives another sort of minimal graph, but not what you want ...

 library("ggthemes")
 g0+theme_tufte()


标签: r ggplot2 axis