bty = “n” in ggplot2

為{幸葍}努か 提交于 2019-12-23 12:50:53

问题


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()


来源:https://stackoverflow.com/questions/33438041/bty-n-in-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!