简体   繁体   中英

Stripchart using Presence/absence over time

Trying to make a stripchart similar to this in R:

示例带状图

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!

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!

Here's a solution using tidyr to reshape the data and then lattice to plot conditional stripplots.

dd_long <- tidyr::gather(df, id, present, -Day)
dd_long$present <- factor(dd_long$present, labels = c("Not present", "Present"))

lattice::stripplot(present ~ Day | id, data = dd_long, layout = c(3, 1))

在此处输入图片说明

Updated answer

Actually, I believe something like the following is more appropriate:

library(tidyr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(lattice)

df <- data.frame(Day = 1:5,
                 ID1 = c(1, 0, 0, 1, 1),
                 ID2 = c(1, 1, 0, 1, 1),
                 ID3 = c(0, 1, 0, 1, 1))

dd_long <- gather(df, id, present, -Day) %>%
  filter(present == 1)

stripplot(id ~ Day, data = dd_long)

Created on 2019-12-07 by the reprex package (v0.3.0)

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