首页 > 解决方案 > 使用混合变量和表达式标记 ggplot

问题描述

我正在尝试在 ggplot 中制作一个带标签的散点图,而标签的细节让我很适应。基本上,除其他外,我想用 R^2 和平均偏差来注释我的 facet_wrapped 2-panel ggplot。值得注意的是,我想用适当的单位标记平均偏差。

我的数据的一个简单版本可能如下所示:

library(tidyverse)

Demo_Df <- tibble(Modeled = rnorm(50,0,1), Observed = rnorm(50, 0.5, 1),
                  Scheme = c(rep("Scheme1", 25), rep("Scheme2", 25)))

Demo_Annotation <- tibble(r.squared = c(0.589, 0.573), Mean_Bias = c(-2.038, -1.049), Scheme = c("Scheme1", "Scheme2"))

Demo_Scatter <- Demo_Df %>%
  ggplot(aes(x = Observed, y = Modeled, color = Scheme)) +
  geom_point(size = 1.5) +
  facet_wrap(~Scheme) +
  theme_tufte() +
  xlab(expression(paste("Observed Aerosol (", mu, "g m" ^ "-3", ")"), sep = "")) +
  ylab(expression(paste("Modeled Aerosol (", mu, "g m" ^ "-3", ")"), sep = "")) +
  ylim(-3, 4) +
  theme(legend.position = "none")

Demo_Labeled <- Demo_Scatter +
  geom_text(data = Demo_Annotation, aes(-2, 3,
            label = paste(
              "R2 = ", sprintf("%.2f", signif(r.squared, 3)), "\n",
              "Mean Bias = ", sprintf("%.2f", signif(Mean_Bias, 3))
              )),
            size = 5, hjust = 0, color = "black")

这产生了几乎正确的数字,但我希望 R2 有一个上标 2,我需要在“平均偏差 =”标签的末尾添加每立方米微克 (ug/m3),因为它在 x和 y 轴。

迄今为止,我在这方面完全失败了。我找不到支持多行facet_wrap、变量输入和 AND 表达式的解决方案。必须有办法做到这一点。请帮助我,tidyverse神!

标签: rggplot2tidyverse

解决方案


实现所需结果的一种选择是通过geom_text多层添加多行。要将标签解析为数学符号,请添加parse=TRUEgeom_text. 最后,我将标签添加到注释 df 中,我在其中使用?plotmath了数学符号。

library(tidyverse)
library(ggthemes)

Demo_Annotation <- Demo_Annotation %>% 
  mutate(r.squared = paste0("R^{2} == ", sprintf("%.2f", signif(r.squared, 3))),
         Mean_Bias = paste0("Mean~Bias == ", sprintf("%.2f", signif(Mean_Bias, 3)), "~mu*g~m^{-3}"))

Demo_Scatter +
    geom_text(data = Demo_Annotation, aes(x = -2, y = 4, label = r.squared),
            size = 5, hjust = 0, color = "black", parse = TRUE, family = "serif") +
    geom_text(data = Demo_Annotation, aes(x = -2, y = 3.5, label = Mean_Bias),
            size = 5, hjust = 0, color = "black", parse = TRUE, family = "serif")

数据

set.seed(42)

Demo_Df <- tibble(Modeled = rnorm(50,0,1), Observed = rnorm(50, 0.5, 1),
                  Scheme = c(rep("Scheme1", 25), rep("Scheme2", 25)))

推荐阅读