How to plot different y with the same x in ggplot?

后端 未结 1 1576
青春惊慌失措
青春惊慌失措 2021-01-29 11:56

Let\'s say I have a dataframe with one column x and other variables y1, y2, ... all continuos.

What\'s the quickest way to plot x ~ y1 and y2 on two different graphs but

相关标签:
1条回答
  • 2021-01-29 12:25

    This type of problems generaly has to do with reshaping the data. The format should be the long format and the data is in wide format.
    I will use the first 3 columns of built in dataset iris as an example dataset.

    library(ggplot2)
    
    df1 <- iris[1:3]
    names(df1) <- c("x", "y1", "y2")
    
    df1_long <- reshape2::melt(df1, id.vars = "x")
    head(df1_long)
    
    ggplot(df1_long, aes(x, value, colour = variable)) +
      geom_point() +
      geom_smooth(method = "lm", se = FALSE) +
      facet_grid(rows = vars(variable))
    

    0 讨论(0)
提交回复
热议问题