I am trying to produce a line graph with 2 lines on, comparing Mean Heart Rate (MHR) and Mean Arterial Blood Pressure (MABP).
When I plot the graph, it pushes the before and after points to the end of the x-axis (right side), how can i change my code so it places these at the start? The code I used is below.
There are many workarounds for this, but a quick and dirty way would be the following:
library(tidyverse)
water <- data.frame(MHR = c(60:67),
MABP = c(78:85),
Time = c("Before", "After", seq(10, 60, by = 10)))
water$Time <- fct_relevel(water$Time, c("Before", "10",
"20", "30", "40", "50",
"60", "After"))
water |>
ggplot(aes(x = Time)) +
geom_line(aes(y=MHR),
group = 1,
colour = "red") +
geom_line(aes(y=MABP),
group = 2,
colour = "blue")
Here is the output:
The reason why your x-axis is not giving you the desired outcome is because you must "relevel" the factors. This is easily customizable by the forcats package. Documentation for this can be found here: