bquote轴标签ggplot2中的换行符和上标
linebreak and superscript in bquote axis label ggplot2
在查看了许多示例并进行了大量尝试之后,我仍然无法将文本字符串和表达式组合到 ggplot2 轴标签中以达到我想要的效果。
我想在这里得到的是 x 轴标签:
成分:
parname <- 'FL.Red.Total'
xmean <- 123.34
xsigma <- 2580.23
要将数字更改为 10^n 符号,我使用此公式:
sci_form10 <- function(x) {
paste(gsub("e\+", " \xB7 10^", scientific_format()(x)))
}
然后将通过以下方式构建名称:
labs( x = bquote(.(gsub('\.', '\ ', parname)) ~ " (a.u.) (" ~ mu ~ "=" ~ .(sci_form10(xmean)) ~ ", " ~ sigma ~ " =" ~ .(sci_form10(xsigma)) ~ ")" ))
我希望将 10^04
替换为 10,然后是上标中的 4,并在标签中添加换行符,如第一张图片所示
测试代码:
library(ggplot2)
library(scales)
sci_form10 <- function(x) {
paste(gsub("e\+", " * 10^", scientific_format()(x)))
}
parname <- 'FL.Red.Total'
xmean <- 123.34
xsigma <- 2580.23
ggplot(mtcars, aes(x=mpg,y=cyl)) +
geom_point() +
labs( x = bquote(.(gsub('\.', '\ ', parname)) ~ " (a.u.) (" ~ mu ~ "=" ~ .(sci_form10(xmean)) ~ ", " ~ sigma ~ " =" ~ .(sci_form10(xsigma)) ~ ")" ))
给出:
p.s。我也试过了
sci_form10 <- function(x) {
paste(gsub(".*e\+", "10^", scientific_format()(x)))
}
它只给出 10^03 部分,看看这是否会改变我的标签的结果,但不会。
一个选项将用 atop
换行以创建换行符
sci_form10 <- function(x) {
paste(gsub("e\+", " \u00B7 10^", scientific_format()(x)))
}
x1 <- sci_form10(xmean)
x2 <- sci_form10(xsigma)
lst1 <- strsplit(c(x1,x2), "\s(?=10)", perl = TRUE)
pre <- sapply(lst1, `[`, 1)
post <- sapply(lst1, `[`, 2)
xmean1 <- parse(text = paste0("'", pre[1], "'"))[[1]]
xsigma1 <- parse(text = paste0("'", pre[2], "'"))[[1]]
post1 <- parse(text = post[1])[[1]]
post2 <- parse(text = post[2])[[1]]
ggplot(mtcars, aes(x=mpg,y=cyl)) +
geom_point() +
labs( x = bquote(atop(.(gsub("\.", "\ ",
parname))~"(a.u.)"~phantom(), "(" ~ mu~ " = "~ .(xmean1) ~ .(post1) ~ ", " ~ sigma ~ " = " ~ .(xsigma1) ~ .(post2)~ ")")))
-输出
我有一些东西可以满足您的大部分需求。
changeSciNot <- function(n) {
output <- format(n, digits=3, scientific = TRUE) # Transforms the number into scientific notation even if small
output <- sub("e", "*10^", output) # Replace e with 10^
output <- sub("\+0?", "", output) # Remove + symbol and leading zeros on exponent, if > 1
output <- sub("-0?", "-", output) # Leaves - symbol but removes leading zeros on exponent, if < 1
output
}
# example data
parname <- "FL.Red.Total"
xmean <- 123.34
xsigma <- 2580.23
label <- bquote(atop(.(gsub("\.", "\ ", parname)) ~ "(a.u.)",
mu*"="*.(changeSciNot(xmean))*"," ~ sigma*"="*.(changeSciNot(xsigma))))
ggplot(mtcars, aes(x=mpg,y=cyl)) +
geom_point() +
labs(x = label)
changeSciNot
函数来自 。我在使用 \xB7
进行乘法时遇到了一些问题,所以我离开了 *
。我还对格式的位数进行了硬编码,但您也可以将其作为参数。希望这将使您更接近所需的确切输出。
在查看了许多示例并进行了大量尝试之后,我仍然无法将文本字符串和表达式组合到 ggplot2 轴标签中以达到我想要的效果。
我想在这里得到的是 x 轴标签:
成分:
parname <- 'FL.Red.Total'
xmean <- 123.34
xsigma <- 2580.23
要将数字更改为 10^n 符号,我使用此公式:
sci_form10 <- function(x) {
paste(gsub("e\+", " \xB7 10^", scientific_format()(x)))
}
然后将通过以下方式构建名称:
labs( x = bquote(.(gsub('\.', '\ ', parname)) ~ " (a.u.) (" ~ mu ~ "=" ~ .(sci_form10(xmean)) ~ ", " ~ sigma ~ " =" ~ .(sci_form10(xsigma)) ~ ")" ))
我希望将 10^04
替换为 10,然后是上标中的 4,并在标签中添加换行符,如第一张图片所示
测试代码:
library(ggplot2)
library(scales)
sci_form10 <- function(x) {
paste(gsub("e\+", " * 10^", scientific_format()(x)))
}
parname <- 'FL.Red.Total'
xmean <- 123.34
xsigma <- 2580.23
ggplot(mtcars, aes(x=mpg,y=cyl)) +
geom_point() +
labs( x = bquote(.(gsub('\.', '\ ', parname)) ~ " (a.u.) (" ~ mu ~ "=" ~ .(sci_form10(xmean)) ~ ", " ~ sigma ~ " =" ~ .(sci_form10(xsigma)) ~ ")" ))
给出:
p.s。我也试过了
sci_form10 <- function(x) {
paste(gsub(".*e\+", "10^", scientific_format()(x)))
}
它只给出 10^03 部分,看看这是否会改变我的标签的结果,但不会。
一个选项将用 atop
换行以创建换行符
sci_form10 <- function(x) {
paste(gsub("e\+", " \u00B7 10^", scientific_format()(x)))
}
x1 <- sci_form10(xmean)
x2 <- sci_form10(xsigma)
lst1 <- strsplit(c(x1,x2), "\s(?=10)", perl = TRUE)
pre <- sapply(lst1, `[`, 1)
post <- sapply(lst1, `[`, 2)
xmean1 <- parse(text = paste0("'", pre[1], "'"))[[1]]
xsigma1 <- parse(text = paste0("'", pre[2], "'"))[[1]]
post1 <- parse(text = post[1])[[1]]
post2 <- parse(text = post[2])[[1]]
ggplot(mtcars, aes(x=mpg,y=cyl)) +
geom_point() +
labs( x = bquote(atop(.(gsub("\.", "\ ",
parname))~"(a.u.)"~phantom(), "(" ~ mu~ " = "~ .(xmean1) ~ .(post1) ~ ", " ~ sigma ~ " = " ~ .(xsigma1) ~ .(post2)~ ")")))
-输出
我有一些东西可以满足您的大部分需求。
changeSciNot <- function(n) {
output <- format(n, digits=3, scientific = TRUE) # Transforms the number into scientific notation even if small
output <- sub("e", "*10^", output) # Replace e with 10^
output <- sub("\+0?", "", output) # Remove + symbol and leading zeros on exponent, if > 1
output <- sub("-0?", "-", output) # Leaves - symbol but removes leading zeros on exponent, if < 1
output
}
# example data
parname <- "FL.Red.Total"
xmean <- 123.34
xsigma <- 2580.23
label <- bquote(atop(.(gsub("\.", "\ ", parname)) ~ "(a.u.)",
mu*"="*.(changeSciNot(xmean))*"," ~ sigma*"="*.(changeSciNot(xsigma))))
ggplot(mtcars, aes(x=mpg,y=cyl)) +
geom_point() +
labs(x = label)
changeSciNot
函数来自 \xB7
进行乘法时遇到了一些问题,所以我离开了 *
。我还对格式的位数进行了硬编码,但您也可以将其作为参数。希望这将使您更接近所需的确切输出。