Split data based on column values and create scatter plot.

后端 未结 2 1405
自闭症患者
自闭症患者 2021-01-19 06:00

I need to make a scatter plot for days vs age for the f group (sex=1) and make another scatter plot for days vs age for the m group (sex=2) using R

相关标签:
2条回答
  • 2021-01-19 06:47

    You can do this with the traditional R graphics functions like:

    plot(age ~ days, Data[Data$sex == 1, ])
    plot(age ~ days, Data[Data$sex == 2, ])
    

    If you prefer to color the points rather than separate the plots (which might be easier to understand) you can do:

    plot(age ~ days, Data, col=Data$sex)
    

    However, this kind of plot would be especially easy (and better looking) using ggplot2:

    library(ggplot2)
    ggplot(Data, aes(x=days, y=age)) + geom_point() + facet_wrap(~sex)
    
    0 讨论(0)
  • 2021-01-19 06:59

    spread splits data by column values. This is also called converting data from "long" to "wide".

    I haven't tested this, but something like

    spread(data, sex, age) 
    

    should get you

    days  1    2
     306  74   NA
     455  NA   67   
    1000  55   NA
     505  65   NA
     399  NA   54   
     495  NA   66 
    
    0 讨论(0)
提交回复
热议问题