koeveon
koeveon

Reputation: 67

bty = "n" in ggplot2

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

Like this:

https://www.google.es/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0CAcQjRxqFQoTCMrel4Gv6sgCFURuFAodNm8DQg&url=http%3A%2F%2Fwww.sthda.com%2Fenglish%2Fwiki%2Fimpressive-package-for-3d-and-4d-graph-r-software-and-data-visualization&bvm=bv.106379543,d.d24&psig=AFQjCNH_hFG9OcD1gDwigFZ1zD-tSpNeCA&ust=1446300403084347

Thank you

Upvotes: 4

Views: 1451

Answers (3)

teunbrand
teunbrand

Reputation: 38053

Apologies for necroing this question, but I thought it might be useful for people revisiting this question to know that ggh4x has an truncated axis guide that does this. (Disclaimer, I wrote that package). Example below:

library(ggplot2)
library(ggh4x)

ggplot(faithful, aes(eruptions, waiting)) +
  geom_point() +
  guides(x = "axis_truncated", y = "axis_truncated") +
  theme(axis.line = element_line(colour = "black"))

Created on 2021-03-22 by the reprex package (v0.3.0)

Upvotes: 0

Ben Bolker
Ben Bolker

Reputation: 226771

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

Upvotes: 3

arvi1000
arvi1000

Reputation: 9592

theme_classic produces something pretty similar to the above

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

enter image description here

Upvotes: 3

Related Questions