r - 使用混合变量和表达式标记 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神!
解决方案
实现所需结果的一种选择是通过geom_text
多层添加多行。要将标签解析为数学符号,请添加parse=TRUE
到geom_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)))
推荐阅读
- javascript - Android:如何在 Webview 中检查 findElementById 是否返回 null?
- javascript - 为什么 console.log(array) 不给出与 console.log({array}) 相同的结果?
- java - 将 Jackson 的 @JsonGetter 与 Lombok 的 @Getter 结合起来
- reactjs - React Styled Components - 使用道具变换比例
- gnuplot - GNUPLOT:使用带有调色板选项的 splot,再现平滑的颜色变化
- php - Doctrine - 当主键不等于外键时如何获取记录
- java - 为什么spring boot项目显示红色标记?
- javascript - 在 JavaScript 中按顺序删除字符串中的所有字符
- c++ - 有人可以在我的代码中找到与 C++ 中的 IF STATEMENT 相关的问题吗?
- linux - xargs + curl “写入正文失败”(不能将多个结果保存到文件中)