Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggtext使用什么格式化语言格式化文本?_R_Ggplot2_R Markdown_Markdown_Ggtext - Fatal编程技术网

R ggtext使用什么格式化语言格式化文本?

R ggtext使用什么格式化语言格式化文本?,r,ggplot2,r-markdown,markdown,ggtext,R,Ggplot2,R Markdown,Markdown,Ggtext,我试图在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_scientifi

我试图在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")) 


库(tidyverse)
图书馆(ggtext)
ggplot(mpg,aes(显示,高速公路*10^9))+geom_点()
#使用“AeB”明确写出Ax10^B的科学符号

fancy_scientific这是一个仅包含
plotmath
表达式的版本:

库(dplyr)
图书馆(GG2)
花式科学
ggplot(aes(显示,硬件))+
主题(经典)+
几何点()
连续缩放(标签=花式科学)+
主题(text=element\u text(face=“bold”))

。。。这是一个带有
ggtext
的版本:

库(dplyr)
图书馆(GG2)
图书馆(ggtext)
花式科学%dplyr::变异(hwy=hwy*1e9)%>%
ggplot(aes(显示,硬件))+
主题(经典)+
几何点()
连续缩放(标签=花式科学)+
主题(text=element\u text(face=“bold”),
axis.text.y=元素\标记(face=“bold”))


由(v0.3.0)创建于2020-08-20,使用Markdown/HTML。您可以只使用unicode字符或使用HTML实体插入特殊字符。在这里,您可能需要
&次

另外,在使用{ggtext}时不要将字符串解析为表达式

库(tidyverse)
图书馆(ggtext)
#使用“AeB”明确写出Ax10^B的科学符号

真想不到,谢谢!您知道一种不指定粗体的方法吗?这种方法给出的是实数乘法符号而不是星号?plotmath版本不要求您在
fancy_scientific
函数外指定粗体,第二个示例的html版本不要求您在函数外指定粗体,我不确定你们想要什么乘法符号,但这也应该没问题。例如,在html符号中使用
gsub((.*)E(\\+?)(\\-?[0-9]+),“**\\1&\\\215;10\\3**”,l)
。谢谢!我是新手,所以我想知道markdown和html之间的区别。例如,您使用插入符号指定上标,而在其他地方(如其他答案)则使用
标记。我假设前者是markdown而后者是HTML,对吗?我还想知道,在编写ggtext输入字符串时,您是否必须选择其中一个,或者在某些情况下,输入可以是标记和html的混合。输入始终作为标记处理,您可以根据需要插入html标记。请参见此处示例:然而,ggtext只知道所有HTML标记中的一小部分,因此不要加入HTML表或列表,并期望它能起作用。