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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:22:45+00:00 2026-06-13T23:22:45+00:00

I have a big table of data with ~150 columns. I need to make

  • 0

I have a big table of data with ~150 columns. I need to make a series of histograms out of about 1/3rd of them. Rather than putting 50 lines of the same plot command in my script, I want to loop over a list telling me which columns to use. Here is a test dataset to illustrate:

d <- data.frame(c(rep("A",5), rep("B",5)),
                sample(c(1:10), 10, replace=TRUE),     
                sample(c(1:10), 10, replace=TRUE),     
                sample(c(1:10), 10, replace=TRUE),     
                sample(c(1:10), 10, replace=TRUE),     
                sample(c(1:10), 10, replace=TRUE))

colnames(d) <- c("col1","col2","col3","col4","col5","col6" )


ggplot(data=d, aes(col2, fill= col1)) + geom_density(alpha = 0.5)

So, rather than writing this a 50 times and replacing the aes() values, I really want to do something more like this…

cols_to_plot <- c("col2","col4","col6")

for (i in length(cols_to_plot)) {
  ggplot(data=d, aes(cols_to_plot[i], fill= col1)) + geom_density(alpha = 0.5)

} 

But of course, this doesn’t work… Is there a way to do this kind of thing?

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-06-13T23:22:46+00:00Added an answer on June 13, 2026 at 11:22 pm

    I think you’d be better off if you melted your data. Try this:

    library(reshape2)
    d2 <- melt(d, id='col1')
    ggplot(d2, aes(value, fill=col1)) + geom_density(alpha=.5) + facet_wrap(~variable)
    

    Or, if you wanted to do what you originally wanted, use aes_string, like:

    ggplot(data=d, aes_string(cols_to_plot[i], fill='col1')) + geom_density(alpha = 0.5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very big table of measurement data in MySQL and I need
I have a big table with 70 rows and 10 columns. It's a table
Lets say I have a really big table filled with lots of data (say,
i have a big table and need to set CSS-classes on it etc. So
I have a big Table , about 1 million records , and it's increasing
I have a very big table with many columns. Values in some of the
I have a big HTML table (~10K rows, 4 columns, text only) dumped from
I have a big data should be shown in a Table. I use javascript
I have this big data-entry sort of page, a table kind of layout using
I have one big table (with tabular data) queried from a MySQL table. Every

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.