r - 在ggplot中结合geom_rect和facet_grid的麻烦
问题描述
我想在一个简单的情节的每个方面对部分背景进行着色。如果我省略 faceting 并运行 geom_rect + geom_point,预期的结果将如下面的 MRE 所示。如果我省略矩形并运行 geom_point + facet_grid,则预期的 4 个面板的每个点都在正确的方面。但是当我结合 geom_rect + geom_point + 和 facet_grid 时,第一类中的点,只有那些点被绘制在每个方面。请问这是怎么回事???
library(ggplot2)
set.seed(42)
syn.dat <- data.frame(
category.1 = as.factor(rep(c("1A", "1B"), each = 8)),
category.2 = as.factor(rep(rep(c("2A", "2B"), times = 2), each = 4)),
x = rep(-1:2, each = 4) + runif(8, max = .4),
y = rep(-1:2, each = 4) + runif(8, max = .4))
ggplot() +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = .5,
ymax = Inf), fill = "lightyellow") +
geom_point(data = syn.dat, aes(x = x, y = y)) +
facet_grid(cols = vars(category.1),
rows = vars(category.2))
解决方案
推荐阅读
- c++ - shell排序中的交换和比较
- json - 在嵌套的 JSON 数组上使用 *ngFor
- node.js - Kubernetes 上 Node.js + gRPC 应用程序的随机超时
- c# - 使用 c# 将字符串写入 XSLT 并保留 xml 标签
- java - 在签名配置中写入密码是否安全?
- javascript - 在点击导航链接上设置类活动
- java - 处理程序调度失败;嵌套异常是 java.lang.AbstractMethodError: error only in WAS 但不在 spring 嵌入式 tomcat 中
- sql - Access 2016 中的转换和透视
- java - Lucene 搜索 SuggestWords()
- python - PyQt - 使用 QListWidget 自定义滚动