Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将几何图元与面一起使用_R_Ggplot2 - Fatal编程技术网

R 将几何图元与面一起使用

R 将几何图元与面一起使用,r,ggplot2,R,Ggplot2,我想绘制一个facet\u网格,其中包含用于facetting的变量组合的不平衡观测值,例如 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

我想绘制一个
facet\u网格
,其中包含用于facetting的变量组合的不平衡观测值,例如

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\u rect
s被绘制在彼此的顶部,即使我根本没有使用
aes()
。我怎样才能防止这种情况发生,使它们在每个方面看起来都一样?

因为您并没有真正使用数据来绘制矩形,所以以后应该使用
注释,这样它就不会与数据或方面相关联。比如说

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)

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)