r - ggplot2 Axis 标签上的数学符号

标签 r math ggplot2 label axis

我想使用 latex2exp 将数学符号添加到 ggplot2 Axis ,但遇到问题,因为 scale_x_discrete() 只接受新标签当名称位于引号之间时,会将 TeX() 函数呈现为文本,如下例所示。如何在更改标签名称的同时包含数学符号?

  p<-ggplot(data=x, aes(x=y, y=x)) +
  geom_errorbar(aes(ymin=x-ci, ymax=x+ci)) +
  scale_x_discrete(breaks=c("label1","label2"),
               labels=c("TeX('$\\alpha^\\beta$')","newlabel2")) 
  p

最佳答案

查看包创建者的这个小插图:

https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html

scale_color_discrete(labels=lapply(sprintf('$\\alpha = %d$', alpha), TeX))

对于您的代码:

p<-ggplot(data=x, aes(x=y, y=x)) +
geom_errorbar(aes(ymin=x-ci, ymax=x+ci)) +
scale_x_discrete(breaks=c("label1","label2"),
               labels = lapply(sprintf('$\\alpha^\\beta$'), TeX)
p

如果您的数学符号不是很复杂,另一个选择是使用 bquote 和 UTF 代码:

mn <- bquote("\u03B1 \u03B2")
labels=c(mn, "newlabel2") 

关于r - ggplot2 Axis 标签上的数学符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49163747/

相关文章:

python - 在 R 中导入 python 模块

r - 这是 mongolite 包中的错误吗?

list - 删除列表中多个数据帧的特定行

r - 如何在密度图中打断 x 轴

java - 如何将奇数索引转换为索引 {0,1,2,3,4,5}?

math - 掌握 GIS 数学,从哪里开始?

R ggplot geom_bar 错误 : Discrete value supplied to continuous scale

javascript - 使用鼠标移动沿斜线移动坐标

R:如何更改 ggplot 中的配色方案(需要 14 种颜色)

r - ggplot2,geom_segment 的颜色段