Same vert / hor axis limitation, fixed aspect, free scaling in faces

To compare the result of several medical variables with a very large difference in the range, I want to create a scatter diagram similar to that below

  • with the same axis horizontally and vertically,

  • fixed aspect, so the slope = 1 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 know of a difficult way to get this with pre-calculated constraints. Setting individual axial limits with facet_wrap and weights = "free" in ggplot2

+6
source share
1 answer

@baptiste, facet_wrap = "free" ggplot2 geom_blank. , ggplots , , .

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

drange = do.call(rbind, by(d, d$what, function(x) extendrange(c(x$v1, x$v2))))
dummy = data.frame(what = rownames(drange), v1 = drange[,1], v2 = drange[,2] )

ggplot(d, aes(x = v1, y = v2 )) +
  geom_point() +
  geom_abline(slope = 1) +
  theme(aspect.ratio = 1) +
  coord_fixed(ratio = 1, expand = FALSE) +
  stat_ellipse() +
  geom_blank(data = dummy) +
  facet_wrap(~what, scales = "free")

enter image description here

, , .

lattice::xyplot(v2~v1|what, data = d, aspect = 1, pch = 16,
                scales = list(relation = "free"),
                panel = function(x,y, ...){
                  lattice::panel.xyplot(x, y, ...) 
                  lattice::panel.abline(a = 0, b = 1)
                },
                prepanel = function(x, y, ...){
                  lim = c(min(x, y), max(x, y))
                  list(xlim = lim, ylim = lim)
                })

enter image description here

0

Source: https://habr.com/ru/post/1017163/


All Articles