Reputation: 67
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
Upvotes: 4
Views: 1451
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
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
Reputation: 9592
theme_classic
produces something pretty similar to the above
ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point(shape=21) +
theme_classic()
Upvotes: 3