Reputation: 486
I am creating a representation of a hospital observation chart in ggplot as part of a project.
I am having some difficulty identifying how to plot a vertical geom_line between the systolic blood pressure and diastolic blood pressure values when the data is held in one column. This is a standard way to represent blood pressure in written notation.
I have plotted the points as symbols in a ggplot using geom_point I now need to link the blood pressure values within each time point.
My ggplot code is as follows:
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")
My data is:
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
The expected output would be a vertical line between the Systolic and Diastolic blood pressure values at each time point.
I have been unable to identify how to select systolic for y and diastolic for yend.
Upvotes: 0
Views: 3156
Reputation: 1248
I'd probably split the line data out to a new data frame using 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)
Then add the line data to the chart you already have:
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")
Upvotes: 3