Search code examples
rstripchart

Stripchart using Presence/absence over time


Trying to make a stripchart similar to this in R:

Example stripchart

Here is some example data (df), where for any given day it gives whether the individual was present (1) or absent (0).

Day  ID1  ID2  ID3
1    1    1    0
2    0    1    1
3    0    0    0
4    1    1    1
5    1    1    1   

I have tried:

stripchart(df)

Which gives nonsense

Have also tried:

stripchart(df ~ rownames(df)  

Which gives errors

I feel like there is a better way to format the data for this but I don't know how!

Any help is much appreciated!


Solution

  • I managed to get some code from someone who did something similar using ggplot()

    Data needs to be in the form of (df):

    Date        ID   Name
    2016-08-11  1    Ray1
    2016-08-12  2    Ray2
    2016-08-12  3    Ray3
    ... etc
    

    with the date (or date time) the individual was present, the ID number of the individual and the Name of the individual (if you want the axis to have names instead of ID number)

    Date needs to be in POSIXct format

    Code is as follows (for only the 3 lines of example data in this answer):

    plot<-ggplot()+ geom_point(data=df, aes(df$Date, y=ID,colour = Name),shape = 19, size = 2)+ scale_y_continuous(breaks=1:3, labels=c("Ray1", "Ray2", "Ray3"))+ scale_x_datetime(date_minor_breaks = "day")+ xlab("Date") + ylab(NULL)+ theme(legend.position="none")+scale_colour_manual(values = c("black", "black"), name = "Ray",breaks = c("Ray1", "Ray2", "Ray3") + theme(legend.position="none"))
    

    Where:

    scale_y_continuous(breaks=1:3, labels=c("Ray1", "Ray2", "Ray3"))
    

    gives breaks the y axis into the 3 individuals and labels them with their names

    scale_x_datetime(date_minor_breaks = "day")
    

    gives the x axis in daily breaks

    scale_colour_manual(values = c("black", "black"), name = "Ray",breaks = c("Ray1", "Ray2", "Ray3") + theme(legend.position="none")
    

    colours the dots black, otherwise it goes rainbow for some reason. Looks cool though :P

    Sorry if I over explained but I had no idea what I was doing so I hope I can help someone else who has no idea what they're doing!

    If anyone has any other suggestions for how to do this style of graph in stripchart I would still like to know!