绘制离散事件的时间线

时间:2019-01-31 19:19:16

标签: r ggplot2

我需要绘制随时间变化的离散事件。我尝试了stripchart(),但对结果不满意,我正在寻找这种图形的ggplot实现。有人可以建议ggplot解决方案吗?

enter image description here

以下是我到目前为止所做的一个简单示例:

a = c("no", "yes", "no", "yes", "no", "maybe", "no", "yes", "maybe") 
b = c(1,2,3,4,5,6,7,8,9) 
mydf = as.data.frame(cbind(a,b)) 
mydf$a = as.factor(mydf$a) 
mydf$b = as.numeric(mydf$b)

stripchart(b ~ a, data=mydf, pch = "x", 
           main="Sequence of fun",
           xlab="Time",
           ylab="Fun?",
           xaxt="n")

1 个答案:

答案 0 :(得分:2)

一种(希望是有趣的方法):

library(tidyverse)
library(emo)
#install.packages("ggthemes")
#devtools::install_github("hadley/emo")

set.seed(54467805)

dat <- data.frame(
  fun  = sample(c('yes', 'maybe', 'no'), 24, replace = T),
  time = 1:24
)

dat %>%
  mutate(fun = fct_relevel(fun, c("no", "maybe", "yes"))) %>%
  mutate(txt = fct_relabel(fun, ~c(ji('poo'), ji('thinking'), ji('happy')))) %>%
  ggplot(aes(x = time, y = fun, label = txt)) +
  geom_text() +
  ggthemes::theme_par() +
  theme(axis.text.y = element_blank(), axis.ticks = element_blank()) +
  labs(x = 'Time', y = 'Fun?')

enter image description here