问题描述
我试图用粗体字在ggplot2轴上显示科学计数法,使用文字“ Ax10 ^ B”格式,而不是ggplot2的默认格式“ AeB”。当这段代码运行时
library(tidyverse)
library(ggtext)
ggplot(mpg,aes(displ,hwy*10^9)) + geom_point()
#makes the scientific notation using "AeB" explicitly write out Ax10^B
fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l,scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e","'\\1'e",l)
# turn the 'e+' into plotmath format
l <- gsub("e","%*%10^",l)
# return this as an expression
parse(text=l)
}
ggplot(mpg,hwy*10^9)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"),axis.text.y = element_markdown(face = "bold"))
我使用element_markdown()
中的ggtext
,因为它可以转移粗体,如我在这里发现的:How do I make ggplot2 custom text formats from axis scale functions follow format specifications set in theme()?
我可以通过将'\\1'
更改为\\1
(删除单引号)来修复双引号。但是我无法显示乘法符号。我可以使用小写的x
,但这很懒。
当我尝试按照此处https://rstudio-pubs-static.s3.amazonaws.com/18858_0c289c260a574ea08c0f10b944abc883.html的建议使用$\times$
时,出现错误。 ggtext
的小插图似乎使用html:https://cran.r-project.org/web/packages/ggtext/vignettes/theme_elements.html,但是它们使用<sup>
标记,这似乎与使用^
的指数相反,而标记没有使用它们时不起作用,而且我搜索的“ html上的乘法符号”的所有资源都未提供解决方案。因此,我的问题是:在哪里可以找到一个好的资源来学习ggtext
/ ggplot2
用于轴刻度标签的正确格式语言?也想知道我遇到的特定问题的解决方案。
解决方法
{ggtext}使用Markdown / HTML。您可以仅使用Unicode字符或使用HTML实体来插入特殊字符。在这里,您可能需要×
。
此外,使用{ggtext}时不要将字符串解析为表达式。
library(tidyverse)
library(ggtext)
#makes the scientific notation using "AeB" explicitly write out Ax10^B
fancy_scientific <- function(l) {
# turn in to character string in scientific notation
l <- format(l,scientific = TRUE)
# quote the part before the exponent to keep all the digits
l <- gsub("^(.*)e","\\1e",l)
# turn the 'e+' into plotmath format
l <- gsub("e","×10^",l)
# return this as a string
l
}
ggplot(mpg,aes(displ,hwy*10^9)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"),axis.text.y = element_markdown(face = "bold"))
由reprex package(v0.3.0)于2020-08-20创建
,这是一个只有plotmath
个表达式的版本:
library(dplyr)
library(ggplot2)
fancy_scientific <- function(l) {
l <- format(l,scientific = TRUE)
parse(text=gsub("(.*)e(\\+?)(\\-?[0-9]+)","bold('\\1') * bold(' * ') * bold('10')^bold('\\3')",l))
}
mpg %>% dplyr::mutate(hwy = hwy * 1e9) %>%
ggplot(aes(displ,hwy)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"))
...这是带有ggtext
的版本:
library(dplyr)
library(ggplot2)
library(ggtext)
fancy_scientific <- function(l) {
l <- format(l,"\\1 * 10^(\\3)",l))
## this would also work,instead of the line above:
# gsub("(.*)e(\\+?)(\\-?[0-9]+)","**\\1 \\* 10<sup>\\3</sup>**",l)
}
mpg %>% dplyr::mutate(hwy = hwy * 1e9) %>%
ggplot(aes(displ,hwy)) +
theme_classic() +
geom_point() +
scale_y_continuous(labels= fancy_scientific) +
theme(text = element_text(face = "bold"),axis.text.y = element_markdown(face = "bold"))
由reprex package(v0.3.0)于2020-08-20创建