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 8970299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:42:38+00:00 2026-06-15T17:42:38+00:00

What is the correct way to assign a ggplot2 grob to a variable if

  • 0

What is the correct way to assign a ggplot2 grob to a variable if the object includes other variabels that I would like to be resolved at the time of assigning.

For example:
xpos and ypos are values that I would like resolved.
I am assigning geom_text to ptext, which I then want to add to some plots, say p1 and p2.

xpos <- .2
ypos <- .7

ptext <- geom_text(aes(x = xpos, y = ypos, label = "Some Text"))

I can add ptext to another plot and everything works as expected

## adding ptext to a plot
p1 <- qplot(rnorm(5), rnorm(5))
p1 + ptext   

However, removing (or altering) xpos & ypos gives undesired results.

rm(xpos, ypos)

p2 <- qplot(rnorm(5), rnorm(5))
p2 + ptext
# Error in eval(expr, envir, enclos) : object 'xpos' not found

What is the correct way to go about this?

  • 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-06-15T17:42:38+00:00Added an answer on June 15, 2026 at 5:42 pm

    You should put xpos and ypos in a data frame. In your example:

    coords = data.frame(xpos=.2, ypos=.7)
    
    ptext <- geom_text(data=coords, aes(x = xpos, y = ypos, label = "Some Text"))
    
    ## adding ptext to a plot
    p1 <- qplot(rnorm(5), rnorm(5))
    p1 + ptext   
    
    # removing (or altering) the old coords data frame doesn't change the plot,
    # because it was passed by value to geom_text
    rm(coords)
    
    p2 <- qplot(rnorm(5), rnorm(5))
    p2 + ptext
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read the PHP Variable page and it states the correct way to assign
How would you correct way to handle an exception in this situation? Initially I
What would be the correct way to create a fully URL-encoded file:// URI from
What is the preferred and/or correct way to release an NSMutableString (or any other
Just to confirm is that the correct way to create designated initializer in Objective-C
What could be a correct way to assign parameters to objects in WPF window
What is the correct way to declare a multidimensional array and assign values to
Possible Duplicate: Local variable assign versus direct assign; properties and memory Which way is
I have a custom Javascript object that I create with new , and assign
Would like to create a method that instantiate objects. - (NSArray *) make3Of :

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.