2

I would like to iterate over a data frame and plot each column against a particular column such as price.

What I have done so far is:

for(i in ncol(dat.train)) {
ggplot(dat.train, aes(dat.train[[,i]],price)) + geom_point()

}

What I want is to have the first introduction to my data (Approximately 300 columns) by plotting against the decision variable (i.e., price)

I know that there is a similar question, though I cannot really understand why the above is not really working.

1
  • use aes(dat.train[[i]],price) instead. Commented May 9, 2017 at 9:28

1 Answer 1

2

You can do this, I have used mtcars data to plot other continuous variables with mpg. You have to melt the data into long form (use gather) and then use ggplot to plot these contiuous variables (disp,drat,qsec etc) against mpg. In your case instead of mpg you would take price and all the other continuous variables to be melted (like here disp,drat,qsec etc), the rest categorical variables can be taken for shape and colors etc (optional).

library(tidyverse)
    mtcars %>%
      gather(-mpg, -hp, -cyl, key = "var", value = "value") %>% 
      ggplot(aes(x = value, y = mpg, color = hp, shape = factor(cyl))) +
      geom_point() +
      facet_wrap(~ var, scales = "free") +
      theme_bw()

EDIT:

This is another solution in case we need separate graphs for each of the variables.

Create a list of variables like this: lyst <- list("disp","hp") , you can use colnames function to get all the variable names. Use lapply to to loop through all the "lyst" objects on your data frame.

setwd("path") ###set the working directory here, This is the place where all the files are saved.

pdf(file=paste0("one.pdf"))
   lapply(lyst, function(i)ggplot(mtcars, aes_string(x=i, y="mpg")) + geom_point())
dev.off()

A pdf file wil. be generated with all the graphs pdfs at your working directory which you have set

Output from solution first:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

It is about 300 columns. I think it would be inevitable to see them all together.
@J.Ze Please request you to mention all the facts in the question, How would someone will know there is so many variables to plot. I will propose a different solution now.
@J.Ze see the EDIT part , I hope this is what you have expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.