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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:19:00+00:00 2026-05-29T04:19:00+00:00

I have to generate 250 plots with the same view. My example data set:

  • 0

I have to generate 250 plots with the same view.

My example data set:

df <- data.frame(name = c("john","alex","mike","dennis","alex"),
             expenses = c("10","12","15","8","2"),
             type = c("food","rent","rent","food","food"))

I would like bar plots with the expenses for every name in a single plot. The plot for “alex” will look like:

selected.name <- "alex"
df1 <- subset(df, name == selected.name)
ggplot(data = df1, aes(type, expenses)) + geom_bar()

Now I want to use a loop that plots the same plot for every name in the df. I have tried to use a for loop that runs the plot code above as a source file. But I can’t pass the name variable to the source file so that it plots the graph for every name. Now I only get one graph out of the for loop.

  • 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-29T04:19:01+00:00Added an answer on May 29, 2026 at 4:19 am

    To answer your orignal question. To do this using standard R:

    doPlot = function(sel_name) {
       dum = subset(df, name == sel_name)
       ggobj = ggplot(data = dum, aes(type, expenses)) + geom_bar()
       print(ggobj)
       ggsave(sprintf("%s.pdf", sel_name))
    }
    lapply(unique(df$name), doPlot)
    

    In this way you end up with a large number of pdf files called Adam.pdf etc. You could then use pdftk (pdf tool kit) to cat the files together in one document. I would still prefer, a better solution using e.g. facetting or a different type of plot.

    Wouldn’t it be much better to use facetting? Given your example the code would be:

    ggplot(data = df, aes(type, expenses)) + 
       geom_bar() + facet_wrap(~name)
    

    which leads to the following plot:

    enter image description here

    Maybe for 250 names and more variables, this might be a problem. But I’d look at facetting nonetheless.

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

Sidebar

Related Questions

I have created a Python file to generate a Mandelbrot set image. The original
I have to generate two random sets of matrices Each containing 3 digit numbers
I have to generate XML in the below format <objects> <items> <item =delete> <searchfields>
For testing purposes I have to generate a file of a certain size (to
I have a new app I'll be working on where I have to generate
I want to call a method, pass it the length and have it generate
I have tried to generate Hello World with openxml sdk. To my surprise when
i'm want to have a repeater generate a bunch of checkboxes, e.g.: <tr><td><input type=checkbox
At work I work closely with MS-Office. I have managed to generate some scripts
We have a requirement to generate PDF documents using information from a PDA /

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.