1

I am new to R and was wondering if there was a way I can simplify my code using an apply function.

In my datafram there are 15 columns - y, a, b, c, d, e, f, g, h, i, j .... o

Instead of:

plot (y~a)
plot (y~b)
.
.
.
plot (y~o)

Is it possible to simplify using an apply function or some other method to enact "plot" over mutiple columns?

1

1 Answer 1

1

There is probably a better solution with but since this is not a reproducible example, here is an approach with matplot():

matplot(x = mtcars[, "carb"], 
        y = mtcars[c("drat", "wt")],
        type = "b",
        pch = 1,
        xlab = "carb",
        ylab = "drat and wt")

The issue with apply() or other base functions is that you would need to determine ahead of time what the xlim and ylim are ahead of time. Otherwise your plots will vary and not look great. That's why ggplot2 has some great features but I would need some sample data, first.

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

Comments

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.