简体   繁体   English

随着时间的推移使用存在/不存在的带状图

[英]Stripchart using Presence/absence over time

Trying to make a stripchart similar to this in R:尝试在 R 中制作类似于此的条形图:

示例带状图

Here is some example data (df), where for any given day it gives whether the individual was present (1) or absent (0).这是一些示例数据 (df),其中对于任何给定的一天,它给出了个人是在场 (1) 还是不在场 (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()我设法从使用 ggplot() 做了类似事情的人那里获得了一些代码

Data needs to be in the form of (df):数据需要采用(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)带有个人出现的日期(或日期时间)、个人的 ID 号和个人的姓名(如果您希望轴具有姓名而不是 ID 号)

Date needs to be in POSIXct format日期需要采用 POSIXct 格式

Code is as follows (for only the 3 lines of example data in this answer):代码如下(仅针对本回答中的 3 行示例数据):

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将 y 轴分成 3 个个体并用他们的名字标记他们

scale_x_datetime(date_minor_breaks = "day")

gives the x axis in daily breaks给出每日休息时的 x 轴

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虽然看起来很酷: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.这是一个使用tidyr重塑数据,然后使用lattice绘制条件条带图的解决方案。

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)reprex 包(v0.3.0) 于 2019 年 12 月 7 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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