在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答案中找到
I am creating a matrix of plots similar to
ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(rows = vars(cyl), cols = vars(drv))
Now, I would like to have some way to highlight some of the individual plots, say the ones where cyl
is 5 or 6, and drv
is f
. So, ideally, this might look like this:
But I would also be happy with those panels having a different look by setting ggtheme
to classic or similar.
However, it is very unclear to me how I can modify individually selected plots within a matrix of plots generated via facet_grid
From @joran answer found here, this is what I get :
[EDIT] code edited to select multiple facets
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)
It does not use theme()
but it seems simple enough to highlight some facets.
这篇关于在ggplot2中使用`facet_grid`时,突出显示/在某些图周围画一个框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!