gpt4 book ai didi

r - 有条件地在 facet_wrap 中填充 ggtext 文本框

转载 作者:行者123 更新时间:2023-12-01 03:02:17 30 4
gpt4 key购买 nike

是否可以有条件地填写这些[ggtext][1]文本框?如果“拾取”,我们说颜色“红色”。

enter image description here

library(cowplot)
library(tidyverse)
library(ggtext)

ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_wrap(~class) +
theme_half_open(12) +
background_grid() +
theme(
strip.background = element_blank(),
strip.text = element_textbox(
size = 12,
color = "white", fill = "#5D729D", box.color = "#4A618C",
halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3)
)
)

最佳答案

您可以拦截元素绘制例程并注入(inject)一些样式更改。

library(ggplot2)
library(cowplot)
library(rlang)
library(ggtext)


element_textbox_highlight <- function(..., hi.labels = NULL, hi.fill = NULL,
hi.col = NULL, hi.box.col = NULL) {
structure(
c(element_textbox(...),
list(hi.labels = hi.labels, hi.fill = hi.fill, hi.col = hi.col, hi.box.col = hi.box.col)
),
class = c("element_textbox_highlight", "element_textbox", "element_text", "element")
)
}

element_grob.element_textbox_highlight <- function(element, label = "", ...) {
if (label %in% element$hi.labels) {
element$fill <- element$hi.fill %||% element$fill
element$colour <- element$hi.col %||% element$colour
element$box.colour <- element$hi.box.col %||% element$box.colour
}
NextMethod()
}


ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_wrap(~class) +
theme_half_open(12) +
background_grid() +
theme(
strip.background = element_blank(),
strip.text = element_textbox_highlight(
size = 12,
color = "white", fill = "#5D729D", box.color = "#4A618C",
halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3),
# this is new relative to element_textbox():
hi.labels = c("minivan", "suv"),
hi.fill = "#F89096", hi.box.col = "#A6424A", hi.col = "black"
)
)



reprex package 创建于 2020-02-21| (v0.3.0)

编辑 :根据@Claus_Wilke 的反馈扩展到 3 种(或更多颜色)
library(ggplot2)
library(cowplot)
library(rlang)
library(ggtext)


element_textbox_highlight <- function(...,
hi.labels = NULL, hi.fill = NULL,
hi.col = NULL, hi.box.col = NULL,
hi.labels2 = NULL, hi.fill2 = NULL,
hi.col2 = NULL, hi.box.col2 = NULL) {
structure(
c(element_textbox(...),
list(hi.labels = hi.labels, hi.fill = hi.fill, hi.col = hi.col, hi.box.col = hi.box.col,
hi.labels2 = hi.labels2, hi.fill2 = hi.fill2, hi.col2 = hi.col2, hi.box.col2 = hi.box.col2)
),
class = c("element_textbox_highlight", "element_textbox", "element_text", "element",
"element_textbox_highlight", "element_textbox", "element_text", "element")
)
}

element_grob.element_textbox_highlight <- function(element, label = "", ...) {
if (label %in% element$hi.labels) {
element$fill <- element$hi.fill %||% element$fill
element$colour <- element$hi.col %||% element$colour
element$box.colour <- element$hi.box.col %||% element$box.colour
}
if (label %in% element$hi.labels2) {
element$fill <- element$hi.fill2 %||% element$fill
element$colour <- element$hi.col2 %||% element$colour
element$box.colour <- element$hi.box.col2 %||% element$box.colour
}
NextMethod()
}


ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_wrap(~class) +
theme_half_open(12) +
background_grid() +
theme(
strip.background = element_blank(),
strip.text = element_textbox_highlight(
size = 12,
# unnamed set (all facet windows except named sets below)
color = "white", fill = "#5D729D", box.color = "#4A618C",
halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3),
# this is new relative to element_textbox():
# first named set
hi.labels = c("minivan", "suv"),
hi.fill = "#F89096", hi.box.col = "#A6424A", hi.col = "black",
# add second named set
hi.labels2 = c("compact", "pickup"),
hi.fill2 = "green", hi.box.col2 = "#A6424A", hi.col2 = "black"
)
)

enter image description here

关于r - 有条件地在 facet_wrap 中填充 ggtext 文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60332202/

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