r - 可视化对话中的眨眼
问题描述
我有显示 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")
但是,该图并没有使关系Utterance
v. blinks 足够清晰:
- 它没有考虑到持续时间
Utterance
- 它无法指示闪烁是否(部分)在开始之前或结束之后
Utterance
发生 - 如果在一次闪烁期间发生了多次闪烁,
Utterance
则将这些闪烁放在不同的行上
所以我正在寻找的是一种既能显示眨眼又能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"))
解决方案
这里的主要问题是,如何塑造数据以便 ggplot 可以绘制它。这是一个建议:本质上我正在替换blink_onset
并blink_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")
推荐阅读
- angular - Angular Reactiveforms 下拉菜单(多个)
- powershell - 在 PowerShell 的文件夹路径中使用变量
- sql - 获取特定或其他语言的页面数据
- java - 如果我在 IF 条件语句中使用 buffer.get(),是否需要使用 buffer.rewind()?
- c# - 无法在c#中发送带有excel附件的邮件
- python - Dask read_sql_table 抛出错误 AttributeError: 'Connection' object has no attribute '_instantiate_plugins'
- java - 这个简单的线程程序是多线程的吗?
- android - 滚动 TVertScrollBox 时防止触发事件
- .net-core - .net core 2.2 facebook auth store access_token
- bash - Bash - 如何在 bash 脚本中的特定行号之后在 rst 文件中输入行