繁体   English   中英

对面使用geom_rect

[英]Using geom_rect with facets

我想绘制一个facet_grid其中包含用于构面的变量组合的不平衡观测值,例如

dat <- data.frame(x = 1:6, 
                  y = 0:5, 
                  group = rep(c("A", "B"), each = 3),
                  var = rep(c("a", "a", "b"), times = 2))

> dat
  group var x y
1     A   a 1 0
2     A   a 2 1
3     A   b 3 2
4     B   a 4 3
5     B   a 5 4
6     B   b 6 5

..并添加geom_rect ,每个方面都应相同。

ggplot(dat) +
  geom_rect(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 3, fill = "red", alpha = .3) +
  geom_point(aes(x = x, y = y)) +
  facet_grid(group~var)

在此处输入图片说明

但是,即使我根本不使用aes() ,似乎还是有几个geom_rect相互绘制。 我该如何防止它们在各个方面看起来都一样?

由于您并没有真正使用数据来绘制矩形,因此您以后应该使用annotate以便它与数据或构面无关。 例如

ggplot(dat) +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 3, fill = "red", alpha = .3) +
  geom_point(aes(x = x, y = y)) +
  facet_grid(group~var)

或者,将数据提供给geom_rect层:

ggplot(dat) +
  geom_rect(aes_all(vars = c('xmin', 'xmax', 'ymin', 'ymax')), fill = "red", alpha = .3,
            data.frame(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 3)) +
  geom_point(aes(x = x, y = y)) +
  facet_grid(group~var)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM