gpt4 book ai didi

r - 在 ggplot2 中使用 `facet_grid` 时突出显示/在一些图周围画一个框

转载 作者:行者123 更新时间:2023-12-03 23:50:43 26 4
gpt4 key购买 nike

我正在创建一个类似于

ggplot(mpg, aes(displ, hwy)) + geom_point() + facet_grid(rows = vars(cyl), cols = vars(drv))
现在,我想用一些方法来突出显示一些单独的图,比如 cyl 的那些图。是 5 或 6,和 drvf .因此,理想情况下,这可能如下所示:
enter image description here
但我也很高兴通过设置 ggtheme 使这些面板具有不同的外观。经典或类似。
但是,我不清楚如何在通过 facet_grid 生成的绘图矩阵中修改单独选定的绘图。

最佳答案

从@joran 找到答案 here ,这就是我得到的:

[编辑] 编辑代码以选择多个方面

    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()但它似乎很简单,可以突出一些方面。

enter image description here

关于r - 在 ggplot2 中使用 `facet_grid` 时突出显示/在一些图周围画一个框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58336449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com