使用 ggcorrplot 相关矩阵上的上标和下标标签

问题描述 投票:0回答:1

我正在尝试自定义使用

ggcorrplot
包制作的相关矩阵上的标签。但是,我无法使上标或下标格式正常工作。如何在不使用 Unicode 字符的情况下使标签正确显示?

library(tidyverse)
library(ggcorrplot)

# Example Data
M <- cor(mtcars)[1:3,1:3]

# Attempt 1: Not formatted correctly
colnames(M) <- c("Temperature\n (^o C)", "PO[4]^2", "Oxygen\n (mg L^-1)")
rownames(M) <- c("Temperature\n (^o C)", "PO[4]^2", "Oxygen\n (mg L^-1)")

ggcorrplot(M,
           type = "lower",
           lab = TRUE,
           ggtheme = theme_bw())


# Attempt 2: Produces error message
ggcorrplot(M,
           type = "lower",
           lab = TRUE,
           scale_x_discrete(labels = c("mpg" = expression(Temperature~(degree*C)),
                                       "cyl" = expression(PO[4]^2),
                                       "disp" = expression(Oxygen~(mg~L^-1)))),
           scale_y_discrete(labels = c("mpg" = expression(Temperature~(degree*C)),
                                       "cyl" = expression(PO[4]^2),
                                       "disp" = expression(Oxygen~(mg~L^-1)))),
           ggtheme = theme_bw())
#> Error in match.arg(method): 'arg' must be NULL or a character vector

创建于 2024-04-23,使用 reprex v2.1.0

r ggplot2 ggcorrplot
1个回答
0
投票

使用

markdown
格式的 ggtext 选项:

library(tidyverse)
library(ggcorrplot)
library(ggtext)

M <- cor(mtcars)[1:3,1:3]

colnames(M) <- c("Temperature\n (<sup>o</sup> C)", "PO<sub>4</sub><sup>2</sup>", "Oxygen\n (mg L<sup>-1</sup>)")
rownames(M) <- c("Temperature\n (<sup>o</sup> C)", "PO<sub>4</sub><sup>2</sup>", "Oxygen\n (mg L<sup>-1</sup>)")

ggcorrplot(M,
           type = "lower",
           lab = TRUE,
           ggtheme = theme_bw()) +
  theme(axis.text = element_markdown())

创建于 2024-04-23,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.