use*_*755 6 r ggplot2 facet-grid
我正在创建一个类似于以下的图矩阵
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(rows = vars(cyl), cols = vars(drv))
Run Code Online (Sandbox Code Playgroud)
现在,我想有一些方法来突出显示一些单独的图,例如那些cyl
是 5 或 6 以及drv
是 的图f
。因此,理想情况下,这可能看起来像这样:
但我也很高兴那些面板通过设置ggtheme
为经典或类似而具有不同的外观。
然而,我非常不清楚如何修改通过生成的图矩阵中单独选择的图facet_grid
从这里找到的 @joran 答案中,这就是我得到的:
[编辑] 编辑代码以选择多个方面
if(!require(tidyverse)){install.packages("tidyverse")}
library(tidyverse)
#dummy dataset
df = data.frame(type = as.character(c("a", "b", "c", "d")),
id = as.character(c("M5", "G5", "A7", "S3")),
val = runif(4, min = 1, max = 10),
temp = runif(4))
# use a rectangle to individually select plots
ggplot(data = df, aes(x = val, y = temp)) +
geom_point() +
geom_rect(data = subset(df, type %in% c("b", "c") & id %in% c("A7","G5")),
fill = NA, colour = "red", xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf) +
facet_grid(type~id)
Run Code Online (Sandbox Code Playgroud)
它没有使用theme()
,但看起来很简单,足以突出一些方面。