Cannot concatenate more than 3 elements in an expression for ggplot2's geom_text

前端 未结 1 528
[愿得一人]
[愿得一人] 2021-01-18 06:38

I have a data frame for which I\'m computing a linear model and would like to include the correlation coefficient and its significance using geom_text.

struc         


        
1条回答
  •  一整个雨季
    2021-01-18 07:24

    pp + geom_text(aes(x=20, y=-5,
      label=paste("list(italic(r) ==", round(val$estimate, digits=2), ", p < 0.0001)")),
      parse=TRUE, colour="black")
    

    The key is that the label argument is parsed if parse==TRUE, this means that the texts need to have a same format as in ?plotmath.

    What the geom_text exactly do is like this:

    expr <- parse(text=label)
    

    and then draw text using the expr as a label. So label argument need to be a valid expression. In you example,

    paste("italic(r) ==", 3, "Q", sep=" ")
    

    is invalid expression, so

    parse(text=paste("italic(r) ==", 3, "Q", sep=" ")) 
    

    induces an error.

    In plotmath, if you want to concat symbols, then you need to use:

    paste(x, y, z)
    list(x, y, z)
    

    So if you want to simply concat, then

    geom_text(foobar, label=paste("paste(italic(r) ==", 3, "Q)", sep=" "))
    

    The first (outside) paste concats a piece of texts into one text variable. The second (inside) paste is used in plotmath process.

    In my example above, I used list (see ?plotmath) instead of paste, because stats and p value is separated by `,'.

    0 讨论(0)
提交回复
热议问题