当数据在同一列中时,如何绘制geom_line的起点和终点

时间:2019-05-09 11:00:18

标签: r ggplot2

作为项目的一部分,我正在ggplot中创建医院观察图的表示形式。

当将数据保存在一列中时,我很难确定如何在收缩压和舒张压之间绘制垂直的geom_line。这是用书面符号表示血压的标准方法。

我已使用geom_point在ggplot中将这些点绘制为符号,现在需要链接每个时间点内的血压值。

我的ggplot代码如下:

obschart <- obs %>% ggplot() +
  geom_point(aes(x=clinical_event_end_date_time, y=value, shape=point_shape))+
  ylab(label = "Value") +
  xlab(label = "Date / Time of observation")

我的数据是:

clinical_event,value,clinical_event_end_date_time,point_shape  
Diastolic Blood Pressure,71,02/01/2019 02:24,triangle down  
Diastolic Blood Pressure,76,02/01/2019 04:22,triangle down  
GCS Total,14,02/01/2019 02:24,square plus  
GCS Total,14,02/01/2019 03:42,square plus  
GCS Total,15,02/01/2019 04:22,square plus  
Heart Rate Monitored,48,02/01/2019 02:24,circle filled  
Heart Rate Monitored,56,02/01/2019 03:42,circle filled  
Heart Rate Monitored,62,02/01/2019 04:22,circle filled  
NEWS Total,2,02/01/2019 04:22,square cross  
NEWS Total,4,02/01/2019 02:24,square cross  
Peripheral Pulse Rate,48,02/01/2019 02:24,circle filled  
Peripheral Pulse Rate,56,02/01/2019 03:42,circle filled  
Peripheral Pulse Rate,62,02/01/2019 04:22,circle filled  
Respiratory Rate,16,02/01/2019 04:22,cross  
Respiratory Rate,17,02/01/2019 03:42,cross  
Respiratory Rate,18,02/01/2019 02:24,cross  
SpO2,95,02/01/2019 02:24,circle cross  
SpO2,95,02/01/2019 04:22,circle cross  
SpO2,96,02/01/2019 03:42,circle cross  
Systolic Blood Pressure,126,02/01/2019 02:24,triangle  
Systolic Blood Pressure,133,02/01/2019 04:22,triangle  
  

预期输出将是每个时间点的收缩压和舒张压值之间的垂直线。

我一直无法确定如何选择y的收缩压和JPYd的舒张压。

1 个答案:

答案 0 :(得分:2)

我可能会使用dplyr将行数据拆分为一个新的数据帧:

library(dplyr)
lines <- obs %>%
  filter(clinical_event %in% c("Diastolic Blood Pressure","Systolic Blood Pressure")) %>%
  select(-point_shape) %>%
  spread(key=clinical_event, value=value) %>%
  rename(dia=2,sys=3)

然后将折线数据添加到您已经拥有的图表中:

ggplot() +
  geom_point(data = obs, aes(x=clinical_event_end_date_time, y=value, shape=point_shape))+
  geom_segment(data = lines, aes(x=clinical_event_end_date_time, xend=clinical_event_end_date_time,
                                 y=dia, yend=sys)) +
  ylab(label = "Value") +
  xlab(label = "Date / Time of observation")