简体   繁体   中英

R: Split data in ggplot based on other factor

I am a beginner with R so I don't have much experience. I ran into a problem when trying to split my scatterplot in groups based on infection status. My dataset consists of log transformed antibody levels logapfhap2 in this example. Infection status any Pf inf is coded as Yes or No and gives information on if someone has been infected during the follow-up period. I am plotting timepoints (x) against antibody levels (y). For time point 1 and 14 I would like to make 2 groups based on infection status.

This is the main part of the code I use to plot the data without splitting in groups:

ggplot() + 
    geom_jitter(data=data2, aes(x='1', y=logapfhap2, colour='PfHAP2A')) + 
    geom_jitter(data=data2,aes(x='14', y=logbpfhap2, colour='PfHAP2B')) + 
    geom_jitter(data=TRC, aes(x='C', y=PfHAP2, colour='PfHAP2C'))

which results in this graph:

在此输入图像描述

Then I tried to split it (I only show the first time point here) which returns an error.

ggplot() + 
    geom_jitter(data=data2[data2$any_Pf_inf=='Yes'], 
                aes(x='1inf', y=logapfhap2[data2$any_Pf_inf=='Yes'], 
                colour='PfHAP2A')) + 
    geom_jitter(data=data2[data2$any_Pf_inf=='No'], 
                aes(x='1un', y=logapfhap2[data2$any_Pf_inf=='No'], 
                colour='PfHAP2B')) 

I wanted to create this graph 在此输入图像描述 but I get this error:

Error: Length of logical index vector must be 1 or 55, got: 482

Hope this is clear! Could anyone help me with this problem? Thanks!

EDIT Not sure if this makes it clearer, but this is what my data looks like: 在此输入图像描述 在此输入图像描述

I just tried some other things and I have solved it now!

ggplot()+ 
      geom_jitter(data=data2[data2$any_Pf_inf=='Yes',], 
          aes(x='1inf', y=logapfhap2, 
          colour='PfHAP2A')) + 
      geom_jitter(data=data2[data2$any_Pf_inf=='No',],
          aes(x='1un', y=logbpfhap2, 
          colour='PfHAP2B'))

Apparently you have to add a comma after [data2$any_Pf_inf=='Yes',] to extract rows instead of columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM