Marco_CH
Marco_CH

Reputation: 3294

Facet, plot axis on all outsides

Is there a way to mirror the x-/y-axis to the opposite side (only outside, no axis on the inside)? My goal is to get something like:

enter image description here


MWE

library(ggplot2)

ggplot(data = iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  geom_point() +
  facet_grid(rows = "Species")

Upvotes: 1

Views: 32

Answers (2)

Lucca Nielsen
Lucca Nielsen

Reputation: 1884

This should work:

ggplot(data = iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  geom_point() +
  scale_y_continuous(sec.axis = sec_axis(~.*1))+
  scale_x_continuous(sec.axis = sec_axis(~.*1))+
  facet_grid(rows = "Species")

enter image description here

Upvotes: 3

teunbrand
teunbrand

Reputation: 37953

Yes, you can use the guides() function to set axes for secondary positions as well, see example below. To have it more like your example, the strip placement should be changed to "outside".

library(ggplot2)

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  facet_grid(rows = vars(Species)) +
  guides(
    x.sec = "axis", y.sec = "axis"
  ) +
  theme(strip.placement = "outside")

Created on 2022-06-07 by the reprex package (v2.0.0)

Upvotes: 2

Related Questions