【问题标题】:parse two signs (==4==2*2) in ggplot2's annotate解析ggplot2的注释中的两个符号(==4==2*2)
【发布时间】:2015-10-14 18:25:37
【问题描述】:

是否有一种简单的方法可以在 ggplot2 的注释中解析同一文本中的两个等号,还是我必须注释两次? (即在下图中将: 替换为=?)

我可以用这样的注释进行绘图

require(ggplot2)
f <- data.frame(x = c(0, 1/1.1), y = c(1/.9, 0))
c <- ggplot(f, aes(x, y))
c + stat_smooth(method = "lm", se=F)  
+ annotate("text", x=.6,y=.75, label=(paste0("slope:","frac(1, f[1])==", 
coef(lm(f$y ~ f$x))[2])), parse=TRUE)

这给了我这个,

但我不能像这样有两个等号,

f <- data.frame(x = c(0, 1/1.1), y = c(1/.9, 0))
c <- ggplot(f, aes(x, y))
c + stat_smooth(method = "lm", se=F)  + annotate("text", x=.6,y=.75,
label=(paste0("slope==","frac(1, f[1])==", coef(lm(f$y ~ f$x))[2])), parse=TRUE)

我收到此错误。

Error in parse(text = lab) : <text>:1:23: unexpected '=='
1: slope==frac(1, f[1])==
                          ^

在我的真实案例中,我有几个分数,我意识到我的工作示例非常简单,有人可能会问为什么用= 替换:,但这是一个工作示例。

【问题讨论】:

    标签: r parsing plot ggplot2 annotations


    【解决方案1】:
    f <- data.frame(x = c(0, 1/1.1), y = c(1/.9, 0))
    c <- ggplot(f, aes(x, y))
    c <- c + stat_smooth(method = "lm", se=F) + annotate("text", x=.6,y=.75, label=(paste0("slope==~frac(1,f[1])==", coef(lm(f$y ~ f$x))[2])), parse=TRUE)
    
    plot(c)
    

    已编辑。剧情是:

    【讨论】:

    • 做得很好,~ 在做什么? ?plotmath 是 POS
    • @罗夏。我认为它增加了一个空间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2020-01-10
    • 2015-09-06
    • 1970-01-01
    相关资源
    最近更新 更多