英文:
Plot crosses on time axis for a binary dependent variable
问题
我想使用ggplot2来绘制随时间变化的二元变量,以便每当我们观察到给定个体(id)的事件(y = 1)时,x轴上会有一个交叉点(或点)。
这是一些数据:
access <- rbinom(n=500,size=1,prob=0.40)
id <- rep(1:10, each = 50)
day_id <- rep(1:50, 10)
da <- data.frame(access = access, id = id, day_id = day_id)
目前我正在使用ggplot2的以下方式:
da %>% ggplot(aes(x = day_id, y = access)) + geom_point() + facet_wrap(~id)
这会导致这个结果:
但我希望它看起来像这样(交叉点或点反映是否有访问权限;x轴反映每个个体的50天序列):
其中每条线代表一个id。
英文:
I want to use ggplot2 to plot a binary variable over time, such that whenever we observe the event (y = 1) for a given individual (id), there is a cross (or point) on the x-axis.
Here is some data:
access <- rbinom(n=500,size=1,prob=0.40)
id <- rep(1:10, each = 50)
day_id <- rep(1:50, 10)
da <- data.frame(access = access, id = id, day_id = day_id)
Currently I'm using ggplot2 in the following way:
da %>% ggplot(aes(x = day_id, y = access)) + geom_point() + facet_wrap(~id)
But I want it to look like this (with crosses or points reflecting whether there was access; and the x_axis reflecting a sequence of 50 days per individual):
Where each line represents one id.
答案1
得分: 2
Here is the translated code:
library(ggplot2)
library(dplyr)
da %>% ggplot(aes(x = day_id, y = factor(id))) +
geom_point(data=filter(da, access==1)) +
geom_line() +
labs(y="id") +
theme_classic()
Please note that I've translated the code part, and I won't provide answers to specific translation questions.
英文:
library(ggplot2)
library(dplyr)
da %>% ggplot(aes(x = day_id, y = factor(id))) +
geom_point(data=filter(da, access==1)) +
geom_line() +
labs(y="id") +
theme_classic()
答案2
得分: 2
Points
uid <- unique(da$id)
plot.new()
plot.window(c(0, nrow(da[da$id == uid[1], ])), c(1, length(uid)))
for (i in seq_along(uid)) {
y <- with(da, access[id == uid[i]])
points(seq_along(y), rep.int(length(uid) - i + 1L, length(y)), col=y, pch=20)
lines(seq_along(y), rep.int(length(uid) - i + 1L, length(y)), lwd=1.5)
}
mtext(LETTERS[rev(seq_along(uid))], 2, at=seq_along(uid), las=1)
Crosses
plot.new()
plot.window(c(0, nrow(da[da$id == uid[1], ])), c(1, length(uid)))
for (i in seq_along(uid)) {
y <- with(da, access[id == uid[i]])
points(seq_along(y), rep.int(length(uid) - i + 1L, length(y)), col=y, pch=3)
lines(seq_along(y), rep.int(length(uid) - i + 1L, length(y)), lwd=1.5)
}
mtext(LETTERS[rev(seq_along(uid))], 2, at=seq_along(uid), las=1)
Alternative visualization
split(da$access, da$id) |> rev() |> do.call(what='cbind') |> image(col=0:1, axes=FALSE) +
mtext(LETTERS[rev(unique(da$id))], 2, 1, at=seq.int(0, 1, length.out=length(unique(da$id))), las=1) + box()
Data
set.seed(42)
access <- rbinom(n=500,size=1,prob=0.40)
id <- rep(1:10, each=50)
day_id <- rep.int(1:50, 10)
da <- data.frame(access=access, id=id, day_id=day_id)
<details>
<summary>英文:</summary>
Points
uid <- unique(da$id)
plot.new()
plot.window(c(0, nrow(da[da$id == uid[1], ])), c(1, length(uid)))
for (i in seq_along(uid)) {
y <- with(da, access[id == uid[i]])
points(seq_along(y), rep.int(length(uid) - i + 1L, length(y)), col=y, pch=20)
lines(seq_along(y), rep.int(length(uid) - i + 1L, length(y)), lwd=1.5)
}
mtext(LETTERS[rev(seq_along(uid))], 2, at=seq_along(uid), las=1)
[![enter image description here][1]][1]
*Crosses*
plot.new()
plot.window(c(0, nrow(da[da$id == uid[1], ])), c(1, length(uid)))
for (i in seq_along(uid)) {
y <- with(da, access[id == uid[i]])
points(seq_along(y), rep.int(length(uid) - i + 1L, length(y)), col=y, pch=3)
lines(seq_along(y), rep.int(length(uid) - i + 1L, length(y)), lwd=1.5)
}
mtext(LETTERS[rev(seq_along(uid))], 2, at=seq_along(uid), las=1)
[![enter image description here][2]][2]
*Alternative visualization*
split(da$access, da$id) |> rev() |> do.call(what='cbind') |> image(col=0:1, axes=FALSE) +
mtext(LETTERS[rev(unique(da$id))], 2, 1, at=seq.int(0, 1, length.out=length(unique(da$id))), las=1) + box()
[![enter image description here][3]][3]
----
*Data:*
set.seed(42)
access <- rbinom(n=500,size=1,prob=0.40)
id <- rep(1:10, each=50)
day_id <- rep.int(1:50, 10)
da <- data.frame(access=access, id=id, day_id=day_id)
[1]: https://i.stack.imgur.com/mjiNa.png
[2]: https://i.stack.imgur.com/tYzfi.png
[3]: https://i.stack.imgur.com/VcoKN.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论