我试图在 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, 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"))
我使用element_markdown()
fromggtext
是因为它允许像我在这里发现的那样转移粗体:如何使轴比例函数中的 ggplot2 自定义文本格式遵循主题()中设置的格式规范?
'\\1'
我可以通过更改为\\1
(删除单引号)来修复双引号。但我无法让乘号显示。我可以只使用小写字母x
,但这很懒惰。
当我尝试$\times$
按照此处的建议使用https://rstudio-pubs-static.s3.amazonaws.com/18858_0c289c260a574ea08c0f10b944abc883.html时 出现错误。一个小插图ggtext
似乎使用 html:https ://cran.r-project.org/web/packages/ggtext/vignettes/theme_elements.html但他们使用<sup>
的标签似乎不利于在^
此处制作指数,并且这些标签在我使用它们时不起作用,并且我搜索的“乘法登录 html”的所有资源都没有产生解决方案。所以我的问题是:我在哪里可以找到一个很好的资源来学习ggtext
/ggplot2
用于轴刻度标签的正确格式语言?也想知道我遇到的具体问题的解决方案。