r - 使用 2 个变量的 facet_wrap 更改 strip.text 的颜色

标签 r ggplot2 visualization facet ggthemes

请参见下图,重点关注蓝色条形文本(每个图表的左上角)。

plot of producción of something

当我在 2 个变量上使用facet_wrap 时,我希望底部 strip 文本为黑色,而顶部 strip 文本保持其Royalblue3 颜色。

我尝试使用 ggthemes、strip.text.x/strip.text.y 选项,但不知道如何执行此操作。

这是代码:

    ggplot(aes(Año, Valor, group = DEPP, color = DEPP)) +
  geom_line(size  = 1,
            alpha = 0.6 ) +
  geom_point(shape = 22, 
             size = 1.5, 
             fill = "grey") +
  theme_ipsum() +
  scale_y_continuous(labels = unit_format(unit = "S/", scale = 1)) +
  scale_x_discrete(breaks = c(2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021)) +
  theme(axis.title.y = element_blank(), 
        axis.title.x = element_blank()) + # Delete the title of the y and x axis
  labs(
    title = "Evolución Valor del Volumen de Producción Según Estado del Pescado y Órigen",      
    subtitle = "Valor de volumen (Kg equivalentes) registrado por la respectiva Dirección de Extracción y Procesamiento Pesquero (DEPP)",
    caption = "Fuente: DIREPRO"
  ) +
  guides(color = "none") +
  facet_wrap(DEPP ~reorder(EstadoPescado, -Valor), scales = "free_y", ncol = 3) +
  theme(strip.text.x = element_text(size = 11, colour = "royalblue3")) #Using ggthemes

  ggsave("desembarque_plot.png", 
         path = here("figures"), 
         width = 15*3, height = 8*10, 
         units = "cm")

最佳答案

我正在 ggh4x 的 github 版本上尝试自定义 strip 。该功能尚未出现在 CRAN 上,但您可能会发现它很有用。主要思想是您可以为 strip 提供元素列表。公然模仿斯特凡的例子:

library(ggplot2)
library(ggh4x) # devtools::install_github("teunbrand/ggh4x")

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  facet_wrap2(
    cyl~am,
    strip = strip_themed(
      text_x = elem_list_text(colour = c("royalblue3", "black")),
      by_layer_x = TRUE
    )
  )

reprex package 于 2021 年 8 月 4 日创建(v1.0.0)

(强制性免责声明:我是作者 ggh4x。如果您发现任何错误,现在是报告它们的好时机)

关于r - 使用 2 个变量的 facet_wrap 更改 strip.text 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68656343/

相关文章:

r - countif 像 r 中的 excel 函数

r - 将定制的 ggpairs() 绘图对象累积到列表对象中

r - stat_function 和图例 : create plot with two separate colour legends mapped to different variables

machine-learning - 知道我有 9 个属性,如何可视化我的数据集?

C# Com Interop 与 Windows Media Player 可视化(带有示例代码)

python - 如何对热图的 numpy 系数数组进行聚类

r - 使用 forms() 更改其他人编写的 R 函数的默认参数时如何处理省略号 (...)

r - 使用 ggplot 禁用绘图中的悬停文本

r - 如何在 ggplot 中独立定位两个图例

python - Python 中的标量场可视化