在 ggplot2 中使用“facet_grid”时突出显示/在一些图周围画一个框
Highlight / Draw a box around some of the plots when using `facet_grid` in ggplot2
我正在创建类似于
的图矩阵
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(rows = vars(cyl), cols = vars(drv))
现在,我想用一些方法来突出一些单独的图,比如 cyl
是 5 或 6,drv
是 f
的那些。所以,理想情况下,这可能看起来像这样:
但我也很高兴通过将 ggtheme
设置为经典或类似的面板来获得不同的外观。
但是,我不清楚如何在通过 facet_grid
生成的图表矩阵中修改单独选择的图表
从@joran 找到的答案here,这是我得到的:
[EDIT] 代码编辑为 select 多方面
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)
它没有使用 theme()
但它似乎很简单,可以突出显示某些方面。
我正在创建类似于
的图矩阵ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(rows = vars(cyl), cols = vars(drv))
现在,我想用一些方法来突出一些单独的图,比如 cyl
是 5 或 6,drv
是 f
的那些。所以,理想情况下,这可能看起来像这样:
但我也很高兴通过将 ggtheme
设置为经典或类似的面板来获得不同的外观。
但是,我不清楚如何在通过 facet_grid
从@joran 找到的答案here,这是我得到的:
[EDIT] 代码编辑为 select 多方面
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)
它没有使用 theme()
但它似乎很简单,可以突出显示某些方面。