【问题标题】:Mathematical expression in tick mark labels, ggplot2刻度线标签中的数学表达式,ggplot2
【发布时间】:2019-03-07 15:33:17
【问题描述】:

我想使用数学表达式制作刻度线标签。 请参阅下一个示例:

library(tidyverse)    
gl<-30
ggplot(data = data.frame(x = c(-5, 5)), aes(x)) +
      stat_function(fun = dt, args = list(df = 30))+ylab("f(t)")+
      geom_segment(aes(x=qt(.975,gl),xend=qt(.975,gl),y=0,yend=dt(qt(.975,gl),gl)))+
      scale_x_continuous("t", round(c(-5,qt(1-.975,gl),0,qt(.975,gl),5),3), limits=c(-5,5),labels=c("-5.000", "-2.042",  "0"  ,"list(q[0.95]==0.025)",  "5.000"))+
      annotate("segment", x = c(2.2), xend = c(3.8), 
               y = c(0.02), yend = c(.16), colour = "red", size=1, alpha=0.6, arrow=arrow())+
      annotate("segment", x = c(-1), xend = c(-3), 
               y = c(0.02), yend = c(.16), colour = 1, size=1, alpha=0.6, arrow=arrow())+
      stat_function(fun = dt, args = list(df = gl),
                    xlim = c(-5,qt(.975,gl)),
                    geom = "area",fill="red",alpha=0.5)+
        annotate("text", x = c(-3.8,3.8,4), y = c(0.18,0.18,.3), 
               label = c("1-alpha","alpha/2","list(q[0.95]==0.025)"),parse=T , size=4 , fontface="bold")+
      theme_bw()

如果行

scale_x_continuous("t", round(c(-5,qt(1-.975,gl),0,qt(.975,gl),5),3), limits=c(-5,5),labels=c("-5.000", "-2.042",  "0"  ,"list(q[0.95]==0.025)",  "5.000"))

替换为

scale_x_continuous("t", round(c(-5,qt(1-.975,gl),0,qt(.975,gl),5),3), limits=c(-5,5),labels=c("-5.000", "-2.042",  "0"  ,"list(q[0.95]==0.025)",  "5.000"),parse=T)

得到一个错误:

scale_x_continuous("t", round(c(-5, qt(1 - 0.975, gl), 0, qt(0.975, : 未使用的参数 (parse = T)

如何在 scale_x_continuous 中实现数学表达式,就像在 annotate 中实现的那样?

【问题讨论】:

  • "得到一个错误。"您介意编辑您的帖子以包含错误消息吗?
  • @duckmayr 完成。谢谢

标签: r ggplot2 axis-labels


【解决方案1】:

您可以在表达式向量中使用plotmath 表达式来实现这一点。

library(tidyverse)
mf<-55
sdf<-8
weight_lim<-c(30, 110)
xlabels <- expression(
  "-5.000",
  "-2.042",
  "0",
  q[0.95]==0.025,
  "5.000"
)
ggplot(data = data.frame(weight = weight_lim), aes(weight)) +
  stat_function(fun = dnorm, n = 101, args = list(mean = mf, sd = sdf),color=2) +
  geom_segment(aes(x=50,xend=50,y=c(0),yend=c(dnorm(50,mf,sdf))),linetype=2,col=2)+
  stat_function(fun = dnorm, args = list(mean = mf,sd=sdf),
                xlim = c(weight_lim[1],50),
                geom = "area",fill="red",alpha=0.5)+
  ylab("f(weight)") + scale_x_continuous("t", seq(weight_lim[1],weight_lim[2], length.out =5), 
                                         limits=weight_lim, 
                                         labels= xlabels) + 
  theme_bw()

由reprex package (v0.3.0) 于 2020 年 7 月 15 日创建

【讨论】:

    【解决方案2】:
    xlabels <- c(
      ~ "-5.000",
      ~ "-2.042",
      ~ "0",
      ~ list(q[0.95]==0.025),
      ~ "5.000"
    )
    ggplot(......) + ...... + 
      scale_x_continuous("t", round(c(-5,qt(1-.975,gl),0,qt(.975,gl),5),3), 
                         limits=c(-5,5), 
                         labels= xlabels)
    

    【讨论】:

    • 有没有办法自动创建xlabels?如果我有标签向量lbls,如何集成xlabels,例如paste(“~”,lbls)? (这显然行不通)
    • 在 R 版本 4.0.2 中出现错误:grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : invalid math annotation跨度>
    • 问题不是 R 4.0,而是 ggplot2 的更新版本。看到这个答案:stackoverflow.com/a/62923231/4975218
    猜你喜欢
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    相关资源
    最近更新 更多