Help plotting x-axis with numeric and non-numeric values

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.

ggplot() +
geom_line(data = water, mapping = aes(x=Time, y=MHR), group = 1, colour = "red") +
geom_line(data = water, mapping = aes(x=Time, y=MABP), group = 2, colour = "blue")

This is the graph that is produced


There are many workarounds for this, but a quick and dirty way would be the following:


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)) +
            group = 1,
            colour = "red") +
            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:

Thats very helpful thank you

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.