Home > Back-end >  Plot association graph between one continuous dependent variable and several other independent varia
Plot association graph between one continuous dependent variable and several other independent varia

Time:12-16

# 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.

CodePudding user response:

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

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

enter image description here

  • Related