Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same axis limits vert/hor, fixed aspect, free scaling in facets

Tags:

r

ggplot2

To compare the result of multiple medical variables with a very large difference in range I want to create a scatterplot like the one below

  • with per-panel same axis horizontally and vertically,

  • fixed aspect, so that the slope=1 line goes through the corners.

-

library(ggplot2)
set.seed(11)
d = rbind(
  data.frame(what = "a", v1 = rnorm(20)+0.2, v2 = rnorm(20)),
  data.frame(what = "b", v1 = rnorm(20, 100, 10)+20, v2 = rnorm(20, 100,10)))

ggplot(d, aes(x = v1, y = v2 )) +
  geom_point() +
  geom_abline(slope = 1) +
  facet_wrap(~what, scales = "free") +
  theme(aspect.ratio = 1) +
  coord_fixed(ratio = 1) + # No effect?
  stat_ellipse()

enter image description here

I am aware of the hard way to get this with precomputed limits. Setting individual axis limits with facet_wrap and scales = "free" in ggplot2

like image 286
Dieter Menne Avatar asked May 11 '17 09:05

Dieter Menne


1 Answers

If you invisibly plot everything with x and y reversed each facet will have identical x and y axes.

library(ggplot2)
set.seed(11)
d = rbind(
  data.frame(what = "a", v1 = rnorm(20)+0.2, v2 = rnorm(20)),
  data.frame(what = "b", v1 = rnorm(20, 100, 10)+20, v2 = rnorm(20, 100,10)))

ggplot(d ) +
  geom_point(aes(x = v1, y = v2 )) +
  geom_blank(aes(x = v2, y = v1 )) +
  geom_abline(slope = 1) +
  facet_wrap(~what, scales = "free") +
  stat_ellipse(aes(x = v1, y = v2 )) +
  stat_ellipse(aes(x = v2, y = v1 ), alpha=0) 
like image 73
Simon Woodward Avatar answered Nov 09 '22 22:11

Simon Woodward