在时间轴上为二元因变量绘制十字。

huangapple go评论68阅读模式
英文:

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 &lt;- rbinom(n=500,size=1,prob=0.40)
id &lt;- rep(1:10, each = 50)   
day_id &lt;- rep(1:50, 10)
da &lt;- data.frame(access = access, id = id, day_id = day_id)

Currently I'm using ggplot2 in the following way:

  da %&gt;% ggplot(aes(x = day_id, y = access)) + geom_point() + facet_wrap(~id)

This leads to this result:
在时间轴上为二元因变量绘制十字。

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 %&gt;% ggplot(aes(x = day_id, y = factor(id))) + 
  geom_point(data=filter(da, access==1)) +
  geom_line() + 
  labs(y=&quot;id&quot;) + 
  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 &lt;- 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 &lt;- 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 &lt;- 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) |&gt; rev() |&gt; do.call(what=&#39;cbind&#39;) |&gt; 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 &lt;- rbinom(n=500,size=1,prob=0.40)
    id &lt;- rep(1:10, each=50)   
    day_id &lt;- rep.int(1:50, 10)
    da &lt;- 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>



huangapple
  • 本文由 发表于 2023年6月29日 15:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定