Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plot association graph between one continuous dependent variable and several other independent variable on same graph

# Set the seed for reproducibility
set.seed(123)

# Create a data frame with 100 rows and 11 columns
mydata <- data.frame(ID = 1:100,
                     outcome = runif(100),
                     var2 = runif(100),
                     var3 = runif(100),
                     var4 = runif(100),
                     var5 = runif(100),
                     var6 = runif(100),
                     var7 = runif(100),
                     var8 = runif(100),
                     var9 = runif(100),
                     var10 = runif(100))

# View the data frame

ggplot(data=mydata, aes(x=var2, y=outcome))+
  geom_smooth()
  1. Want to create a plot containing all smooth line associations between the 8 var and outcome.
    2 ) each var has a colored line with a legend explaining the colors.

>Solution :

The easiest way to achieve this is to pivot your data into long format and map the color aesthetic to the resulting name variable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ggplot(tidyr::pivot_longer(mydata, -(1:2)), aes(value, outcome)) +
  geom_smooth(aes(color = name), alpha = 0.1)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading