Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 由两位演讲者为演讲和非演讲创建活动情节_R_Ggplot2 - Fatal编程技术网

R 由两位演讲者为演讲和非演讲创建活动情节

R 由两位演讲者为演讲和非演讲创建活动情节,r,ggplot2,R,Ggplot2,我有两个演讲者的语音数据,分别表示他们说话时的开始和结束时间,以及中间的沉默,按分钟s分组: df <- structure(list(speaker = c(NA, "A", NA, NA, "B", NA, "A", NA, NA, "B", "A"), start = c(0, 20000, 35000, 60000, 65000, 8000

我有两个演讲者的语音数据,分别表示他们说话时的
开始
结束
时间,以及中间的沉默,按
分钟
s分组:

df <- structure(list(speaker = c(NA, "A", NA, NA, "B", NA, "A", NA, NA, "B", "A"), 
                     start = c(0, 20000, 35000, 60000, 65000, 80000,1e+05, 110000, 120000, 140000, 180000), 
                     end = c(20000, 35000,60000, 65000, 80000, 1e+05, 110000, 120000, 140000, 180000, 195000), 
                     minute = c(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L)), 
                row.names = c(NA,-11L), class = "data.frame")

这里有一种方法,我将演讲者身份转换为一个数字,并使用该数字从分钟开始垂直偏移

library(ggplot2)
ggplot(df, aes(x = start, xend = end, 
               y = minute + scale(as.numeric(as.factor(speaker)))/10,
               yend = minute + scale(as.numeric(as.factor(speaker)))/10,
               color = speaker)) +
  geom_segment(size = 4) +
  scale_y_reverse(breaks = 0:10, labels = paste0(0:10, "min"), name = NULL)

编辑——根据建议按分钟重新调整计时,并包括NA:

library(tidyverse)
df %>%
  replace_na(list(speaker = "NA")) %>%
  mutate(start = start - 60000*minute,
         end = end - 60000*minute,) %>%
  ggplot(aes(x = start, xend = end, 
               y = minute + scale(as.numeric(as.factor(speaker)))/10,
               yend = minute + scale(as.numeric(as.factor(speaker)))/10,
               color = speaker)) +
  geom_segment(size = 4) +
  scale_y_reverse(breaks = 0:10, labels = paste0(0:10, "min"), name = NULL)

那太好了。为什么不显示NA?另请参见我的edit.updated,这有帮助吗?在我的实际数据中,我收到了以下警告信息:删除了408行,其中包含缺少的值(geom_段).这是为什么?如何防止?听起来408行缺少开始/结束/分钟/扬声器,因此无法为这些行绘制片段。我建议在
ggplot
行前面放一个
View()
,看看它在哪里。
library(tidyverse)
df %>%
  replace_na(list(speaker = "NA")) %>%
  mutate(start = start - 60000*minute,
         end = end - 60000*minute,) %>%
  ggplot(aes(x = start, xend = end, 
               y = minute + scale(as.numeric(as.factor(speaker)))/10,
               yend = minute + scale(as.numeric(as.factor(speaker)))/10,
               color = speaker)) +
  geom_segment(size = 4) +
  scale_y_reverse(breaks = 0:10, labels = paste0(0:10, "min"), name = NULL)