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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:28:19+00:00 2026-05-26T03:28:19+00:00

I have this project where I want to make the same plots for a

  • 0

I have this project where I want to make the same plots for a variety of different data frames. I was thinking I could do this by writing a function that takes a data frame as an input and then returns a vector of plots—something like this:

df <- data.frame(x = runif(100), y = runif(100))
plot.list <- function(df){
  g1 <- qplot(x, y, data = df)
  g2 <- qplot(x, x + y, data = df)
  c(g1, g2) 
}

And I’d like to do this:

print(plot.list(df)[1])

getting the same results as if I had done:

print(qplot(x,y, data = df))

As you’ll see, this doesn’t work—it seems to print out the data frame that the plot is based on (?). My guess is that I’m misunderstanding something pretty basic about how objects work in R or the nature of ggplot2 plots. Thanks for any advice (or perhaps recommendations on better ways to do what I’m trying to do).

  • 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-26T03:28:19+00:00Added an answer on May 26, 2026 at 3:28 am

    There are several ways you could approach this sort of thing, but one trick you may not be aware of is that ggplot2 has a special operator for this sort of purpose, i.e. creating the same graph using different data frames:

    d1 <- data.frame(x=1:10,y=rnorm(10))
    d2 <- data.frame(x=20:29,y=runif(10))
    p <- ggplot(data = d1, aes(x = x, y = y)) + geom_point()
    print(p)
    print(p %+% d2)
    

    So the %+% operator will plug in the data frame d2 into the plot structure defined by p. That way you can create the plot once and then apply it to different data frames.

    To more directly address the use you outline in your question, once you create the first plot, p, you can lapply this plot structure along a list of data frames:

    d3 <- data.frame(x = 1:10, y = rexp(10))
    rs <- lapply(list(d1,d2,d3),FUN = function(x,p){p %+% x},p=p)
    

    And then you have three plots stored in rs.

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

Sidebar

Related Questions

I have a private project, and i want it hosted on google code. this
In a project I need to make headings like this and I want to
I have a project on GitHub behind a private repository. I want to make
Is this possible? I have a solution with both projects, I just want to
I have this project that it's due in a few hours and I still
So I have this project in PHP where I have some include files next
Okay so, I have this project structure: package A.B class SuperClass (this class is
I have found this project on Codeplex. http://www.codeplex.com/ProjNET I need to integrate this code
I have this large C++ project that I need to build on a platform
I have this tiny Qt project with a project file like this: TEMPLATE =

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.