Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3497732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:24:22+00:00 2026-05-18T12:24:22+00:00

I have a data frame for which I’m computing a linear model and would

  • 0

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.

structure(list(ppno = c(1L, 1L, 1L, 10L, 10L, 10L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 
8L, 8L, 9L, 9L, 9L), light.color = structure(c(1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("B", "IR", 
"IR+B"), class = "factor"), session = c(2L, 1L, 3L, 2L, 3L, 1L, 
1L, 3L, 2L, 3L, 2L, 1L, 2L, 3L, 1L, 3L, 1L, 2L, 1L, 2L, 3L, 2L, 
1L, 3L, 1L, 3L, 2L, 3L, 2L, 1L), time = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("pre", 
"post"), class = "factor"), pre.pri.s = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA), pre.pri.r = c(8L, 4L, 6L, 
2L, 2L, 4L, 10L, 12L, 9L, 24L, 16L, 15L, 15L, 15L, 15L, 3L, 5L, 
7L, 13L, 11L, 12L, 16L, 15L, 14L, 21L, 5L, 8L, 1L, 0L, 0L), pre.nwc = c(5L, 
2L, 4L, 2L, 2L, 4L, 10L, 10L, 9L, 11L, 10L, 11L, 12L, 11L, 11L, 
3L, 5L, 6L, 9L, 11L, 12L, 12L, 11L, 10L, 11L, 5L, 8L, 1L, 0L, 
0L), pre.ppi = structure(c(3L, 2L, 2L, 1L, 1L, 2L, 2L, 3L, 2L, 
3L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, NA, 2L, 2L, 3L, 3L, 3L, 4L, 
2L, 3L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4", "NULL"), class = "factor"), 
    pre.pri.nwc = c(1.6, 2, 1.5, 1, 1, 1, 1, 1.2, 1, 2.18181818181818, 
    1.6, 1.36363636363636, 1.25, 1.36363636363636, 1.36363636363636, 
    1, 1, 1.16666666666667, 1.44444444444444, 1, 1, 1.33333333333333, 
    1.36363636363636, 1.4, 1.90909090909091, 1, 1, 1, NaN, NaN
    ), post.pri.s = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA), post.pri.r = c(4L, 4L, 7L, 0L, 0L, 4L, 
    3L, 8L, 7L, 16L, 12L, 19L, 6L, 10L, 4L, 1L, 3L, 0L, 3L, 11L, 
    15L, 8L, 9L, 9L, 8L, 4L, 3L, 0L, 0L, 0L), post.nwc = c(4L, 
    3L, 4L, 0L, 0L, 3L, 3L, 8L, 7L, 10L, 9L, 15L, 5L, 9L, 4L, 
    1L, 3L, 0L, 3L, 8L, 13L, 8L, 9L, 9L, 8L, 4L, 3L, 0L, 0L, 
    0L), post.ppi = structure(c(2L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 3L, 2L, 5L, 1L, 1L, NA, 3L, 2L, 1L, 1L, 
    2L, 3L, 2L, 2L, 1L, 1L, 1L), .Label = c("1", "2", "3", "4", 
    "NULL"), class = "factor"), post.pri.nwc = c(1, 1.33333333333333, 
    1.75, NaN, NaN, 1.33333333333333, 1, 1, 1, 1.6, 1.33333333333333, 
    1.26666666666667, 1.2, 1.11111111111111, 1, 1, 1, NaN, 1, 
    1.375, 1.15384615384615, 1, 1, 1, 1, 1, 1, NaN, NaN, NaN), 
    delta.pri.r = c(4, 0.1, -1, 2, 2, 0.1, 7, 4, 2, 8, 4, -4, 
    9, 5, 11, 2, 2, 7, 10, 0.1, -3, 8, 6, 5, 13, 1, 5, 1, 0.1, 
    0.1), delta.nwc = c(1, -1, 0.1, 2, 2, 1, 7, 2, 2, 1, 1, -4, 
    7, 2, 7, 2, 2, 6, 6, 3, -1, 4, 2, 1, 3, 1, 5, 1, 0.1, 0.1
    ), delta.pri.nwc = c(-0.6, -0.666666666666667, 0.25, NaN, 
    NaN, 0.333333333333333, 0.1, -0.2, 0.1, -0.581818181818182, 
    -0.266666666666667, -0.0969696969696969, -0.05, -0.252525252525252, 
    -0.363636363636364, 0.1, 0.1, NaN, -0.444444444444444, 0.375, 
    0.153846153846154, -0.333333333333333, -0.363636363636364, 
    -0.4, -0.90909090909091, 0.1, 0.1, NaN, NaN, NaN), delta.vas = c(4.081632, 
    -43.877544, -8.163264, -2.040816, 0.510204, 9.183672, 8.163264, 
    8.163264, 11.224488, 0, -14.285712, -11.224488, 19.387752, 
    0, 26.530608, 2.040816, 10.20408, 11.224488, 42.346932, -10.20408, 
    -28.06122, 11.224488, 5.612244, 21.428568, 22.448976, 0, 
    23.469384, 0.510204, -1.020408, 0)), .Names = c("ppno", "light.color", 
"session", "time", "pre.pri.s", "pre.pri.r", "pre.nwc", "pre.ppi", 
"pre.pri.nwc", "post.pri.s", "post.pri.r", "post.nwc", "post.ppi", 
"post.pri.nwc", "delta.pri.r", "delta.nwc", "delta.pri.nwc", 
"delta.vas"), row.names = c(NA, -30L), class = "data.frame")

Using this code for the plot.

p <- ggplot(data=mpq.vas, mapping=aes(x=delta.vas, y=delta.pri.r, 
    colour=light.color)) +
  geom_point() +
  geom_smooth(aes(group=1), method="lm", size=1, colour="black")
#
#  Clean up the basics.
pp <- p + geom_hline(yintercept=0, colour="grey60") + 
  geom_vline(xintercept=0, colour="grey60") +
  scale_colour_manual(name="Treatment\ncolor", values=cols) +
  scale_x_continuous(name=
    expression(paste(Delta, " VAS pain [t(0) - t(60)]")))+
  scale_y_continuous(name=expression(paste(Delta, "PRI(r) [pre - post]")))
#
#  Add correlation info.
val <- cor.test(mpq.vas$delta.vas, mpq.vas$delta.pri.r)

When I then try to add the correlation coefficient somewhere in the text, I get an error about an unexpected symbol at the location of the Q in the label.

pp + geom_text(aes(x=20, y=-5, label=paste("italic(r) ==", 3, "Q", sep=" ")), 
    parse=TRUE, colour="black")

(yes, I know a correlation of 3 is impossible, just an example).

I would like to do:

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

But this generates the same error, now at the \n thingy. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:24:23+00:00Added an answer on May 18, 2026 at 12:24 pm
    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to normalize some data which I have in a data frame. I
I have a data.frame called series_to_plot.df which I created by combining a number of
I have a data frame with several columns, one of which is a factor
I have data that looks like CUSTOMER, CUSTOMER_ID, PRODUCT ABC INC 1 XYX ABC
I have data that looks like this: entities id name 1 Apple 2 Orange
If I have data like this: Key Name 1 Dan 2 Tom 3 Jon
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame containing multiple time series of returns, stored in columns.
I have a data.frame from this code: my_df = data.frame(read_time = c(2010-02-15, 2010-02-15, 2010-02-16,
I have data in a MySQL database. I am sending the user a URL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.