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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:23:52+00:00 2026-05-23T14:23:52+00:00

For my convenience, I want to write a function that adds a line plot

  • 0

For my convenience, I want to write a function that adds a line plot and a scatter plot to an already existing plot. I defined:

addlinetoplot <- function(dataset, varx, vary)
     { 
       p <- geom_line(data=dataset, aes_string(x=varx, y=vary)) + 
            geom_point(data=dataset, aes_string(x=varx, y=vary))

       p
     }

The above function does not work. It works if I remove the + sign and the geom_point() part. The above does not work because for some reason one cannot add those two geoms. The following example illustrates the problem.

Then, I tried:

df1 <- data.frame(c1 = c(1:10), c2 = c(1:10))
c1 <- c(1:10)
csq <- c1^2
df2 <- data.frame(c1 = c(1:10), csq)
pltbase <- ggplot() + geom_line(df1, aes(x="c1", y="c2")) 

# This does not work.
pltbase + addlinetoplot(dataset=df2, varx = "c1", vary = "csq")

I figured the problem is with the statement:

addthistotheplot <- geom_line(data=df2, aes_string(x="c1", y="csq")) + 
                    geom_point(data=df2, aes_string(x="c1", y="csq"))

Trying to define the above statement throws an error: non-numeric argument to binary operator.

How can I define the addlinetoplot() function so that I can make:

pltbase + addlinetoplot(dataset=df2, varx = "c1", vary = "csq")

work. One way is to separate out the geom_line and geom_point and have different function definition for each. Is there a way to do it in the same function?

Thanks!

  • 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-23T14:23:53+00:00Added an answer on May 23, 2026 at 2:23 pm

    I can’t find the reference right now, but there’s an answer on SO somewhere that explains you can add to a ggplot plot by passing new arguments as a list. Accordingly, your function becomes:

    addlinetoplot <- function(dataset, varx, vary) { 
      list(
        geom_line(data=dataset, aes_string(x=varx, y=vary)), 
        geom_point(data=dataset, aes_string(x=varx, y=vary))
      )
    }
    

    And then your plotting code looks like:

    pltbase <- ggplot() + geom_line(data = df1, aes(x=c1, y=c2))
    pltbase + addlinetoplot(df2, varx = "c1", vary = "csq")
    

    Results in:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that outputs something to a ostream that's passed
Suppose I want to write a function in R which is a function of
For convenient usage I want to write formatting function similar to sprintf just returning
I want to write preprocessor functions/arrays that are evaluated at compile time. For example,
I am developing some convenience wrappers around another software package that defines a bash
I often use convenience functions that return pointers to static buffers like this: char*
I have a number of algorithms for community detection on graphs that I want
I had previously written a function that seems to work, but unfortunately I didn't
I recently became aware that the strdup() function I've enjoyed using so much on
I'm trying to do some parameter estimation and want to choose parameter estimates that

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.