首页 > 解决方案 > 可视化对话中的眨眼

问题描述

我有显示 ppl眨眼时的瞳孔数据的对话数据,如下所示(可重复的数据如下):

df
# A tibble: 6 x 8
# Groups:   Blinks_grp [6]
  Speaker Utterance  Starttime_ms Endtime_ms Blink_onset Blink_offset Blinks_grp Blink_dur
  <chr>   <chr>             <int>      <dbl>       <dbl>        <dbl>      <dbl>     <dbl>
1 ID16.B  an Americ…       289569     293940      289879       289946        113        67
2 ID16.B  an Americ…       289569     293940      290696       290879        114       183
3 ID16.B  an Americ…       289569     293940      290962       291046        115        84
4 ID16.A  [°gotcha°]       290604     291004      290696       290879        116       183
5 ID16.B  =↓my fath…       300938     302140      301529       301612        117        83
6 ID16.B  =↓my fath…       300938     302140      302062       302146        118        78

我想可视化何时发生相对于语音的眨眼(在列中Utterance)。到目前为止,我已经想出了这段代码:

df %>%
  mutate(Utterance = paste0(sub(".*(.)$", "\\1",Speaker), ": ", Utterance),
         Utterance = factor(Utterance, levels = unique(Utterance))) %>%
  ggplot(aes(x = Blink_onset, xend = Blink_offset,
             y = Blinks_grp, yend = Blinks_grp)) +
  geom_segment(size = 3) +
  facet_wrap(~ Utterance, ncol = 1, scales= "free_x")

这产生了这个图: 在此处输入图像描述

但是,该图并没有使关系Utterancev. blinks 足够清晰:

所以我正在寻找的是一种既能显示眨眼又能Utterance清楚地显示眨眼相对于Utterance. 我的想法看起来有点像这样:

在此处输入图像描述

可重现的数据:

structure(list(Speaker = c("ID16.B", "ID16.B", "ID16.B", "ID16.A", 
"ID16.B", "ID16.B"), Utterance = c("an American family that (.) [uh] moved to Germany in <nineteen ninety one>", 
"an American family that (.) [uh] moved to Germany in <nineteen ninety one>", 
"an American family that (.) [uh] moved to Germany in <nineteen ninety one>", 
"[°gotcha°]", "=↓my father's↓ like ~°we're going to Germany°~", 
"=↓my father's↓ like ~°we're going to Germany°~"), Starttime_ms = c(289569L, 
289569L, 289569L, 290604L, 300938L, 300938L), Endtime_ms = c(293940, 
293940, 293940, 291004, 302140, 302140), Blink_onset = c(289879, 
290696, 290962, 290696, 301529, 302062), Blink_offset = c(289946, 
290879, 291046, 290879, 301612, 302146), Blinks_grp = c(113, 
114, 115, 116, 117, 118), Blink_dur = c(67, 183, 84, 183, 83, 
78)), row.names = c(NA, -6L), groups = structure(list(Blinks_grp = c(113, 
114, 115, 116, 117, 118), .rows = structure(list(1L, 2L, 3L, 
    4L, 5L, 6L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

标签: rggplot2dplyr

解决方案


这里的主要问题是,如何塑造数据以便 ggplot 可以绘制它。这是一个建议:本质上我正在替换blink_onsetblink_offset使用一个属性来告诉表中的条目是闪烁还是说话,然后 ggplot 可以轻松地为每个事件绘制一条单独的行:

## Preparing the data:

df <- structure(list(Speaker = c("ID16.B", "ID16.B", "ID16.B", "ID16.A", 
"ID16.B", "ID16.B"), Utterance = c("an American family that (.) [uh] moved to Germany in <nineteen ninety one>", 
"an American family that (.) [uh] moved to Germany in <nineteen ninety one>", 
"an American family that (.) [uh] moved to Germany in <nineteen ninety one>", 
"[°gotcha°]", "=↓my father's↓ like ~°we're going to Germany°~", 
"=↓my father's↓ like ~°we're going to Germany°~"), Starttime_ms = c(289569L, 
289569L, 289569L, 290604L, 300938L, 300938L), Endtime_ms = c(293940, 
293940, 293940, 291004, 302140, 302140), Blink_onset = c(289879, 
290696, 290962, 290696, 301529, 302062), Blink_offset = c(289946, 
290879, 291046, 290879, 301612, 302146), Blinks_grp = c(113, 
114, 115, 116, 117, 118), Blink_dur = c(67, 183, 84, 183, 83, 
78)), row.names = c(NA, -6L), groups = structure(list(Blinks_grp = c(113, 
114, 115, 116, 117, 118), .rows = structure(list(1L, 2L, 3L, 
    4L, 5L, 6L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

df <- df %>%
  mutate(Utterance = paste0(sub(".*(.)$", "\\1",Speaker), ": ", Utterance),
         Utterance = factor(Utterance, levels = unique(Utterance)))

## separate into a "Blink" and a "talk" data frame, add an attribute "Event" that represents talking and blinking:
blink_df <- df %>% select(Speaker, Utterance, Blink_onset, Blink_offset, Blinks_grp) %>%
    mutate(Starttime_ms = Blink_onset, Endtime_ms = Blink_offset, Event = "blink")

talk_df <- df %>% select(Speaker, Utterance, Starttime_ms, Endtime_ms, Blinks_grp) %>%
    mutate(Event = "talk")

## combine datasets again:
plot_df <- bind_rows(talk_df, blink_df)

## and plot, using "Event" as attribute to separate talking and blinking lines:
plot_df %>% 
    ggplot(aes(x = Starttime_ms, xend = Endtime_ms,
             y = Event, yend = Event, colour = Event)) +
  geom_segment(size = 3) +
  facet_wrap(~ Utterance, ncol = 1, scales= "free_x")

推荐阅读