r 添加数学符号到 x 轴文本
r add math symbol to x axis text
我正在尝试向 x 轴上的某些文本添加数学符号(总和)。
例如,如果这是我的情节。
data(iris)
p1 <- ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot()
p1
我要完成的就是这个。不知道该怎么做,任何建议表示赞赏。提前致谢。
这几乎可以肯定是重复的,但我在搜索时找不到答案,所以这是一个可能的解决方案:
library(tidyverse)
iris_correctly_labelled <- iris %>%
mutate(Species = case_when(Species == "setosa" ~ paste0("\u2211", "setosa"),
Species == "versicolor" ~ "versicolor",
Species == "virginica" ~ paste0("\u2211", "virginica")))
p1 <- ggplot(iris_correctly_labelled,
aes(x=fct_reorder(Species, Sepal.Length), y=Sepal.Length)) +
geom_boxplot() +
theme(axis.title.x = element_blank(),
text = element_text(size = 16))
p1
由 reprex package (v2.0.1)
创建于 2022-05-13
这是否解决了您的问题?
您还可以考虑使用 latex2exp
包的 TeX
函数,它允许您使用 LaTeX 的语法添加更复杂的符号和公式来绘制文本和标题:
data(iris)
library(ggplot2)
library(latex2exp)
x_labs <- c(TeX(r'($\Sigma$ setosa)'), "versicolor", TeX(r'($\Sigma$ virginica)'))
p1 <- ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
scale_x_discrete(labels = x_labs) +
theme() # adjust text size to suit
p1
我正在尝试向 x 轴上的某些文本添加数学符号(总和)。
例如,如果这是我的情节。
data(iris)
p1 <- ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot()
p1
我要完成的就是这个。不知道该怎么做,任何建议表示赞赏。提前致谢。
这几乎可以肯定是重复的,但我在搜索时找不到答案,所以这是一个可能的解决方案:
library(tidyverse)
iris_correctly_labelled <- iris %>%
mutate(Species = case_when(Species == "setosa" ~ paste0("\u2211", "setosa"),
Species == "versicolor" ~ "versicolor",
Species == "virginica" ~ paste0("\u2211", "virginica")))
p1 <- ggplot(iris_correctly_labelled,
aes(x=fct_reorder(Species, Sepal.Length), y=Sepal.Length)) +
geom_boxplot() +
theme(axis.title.x = element_blank(),
text = element_text(size = 16))
p1
由 reprex package (v2.0.1)
创建于 2022-05-13这是否解决了您的问题?
您还可以考虑使用 latex2exp
包的 TeX
函数,它允许您使用 LaTeX 的语法添加更复杂的符号和公式来绘制文本和标题:
data(iris)
library(ggplot2)
library(latex2exp)
x_labs <- c(TeX(r'($\Sigma$ setosa)'), "versicolor", TeX(r'($\Sigma$ virginica)'))
p1 <- ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
scale_x_discrete(labels = x_labs) +
theme() # adjust text size to suit
p1