How can I change dates on an X axis into 'day 1', 'day 2' etc for a line graph plot?

83 views Asked by At

I am trying to modify a line graph i have already made. On the x axis, it has the data in which a participant completed a task. However, I am trying to make it so the x axis simply show each completed session of the task as day 1, day 2 etc.... Is there a way to do this?

My code for the line graph is as follows:

ggplot(data = p07_points_scored, aes(x = day, y = total_score, group = 1)) +
  geom_line() +
  geom_point() +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5)) +
  labs(title=" P07s Total score on the training tool",
       x = "Date of training completion",
       y = "Total Score",
       color = "lightblue") +
  geom_smooth()

To further add to this. I have 4 separate line graphs from individual participants showing their total scores within the task. Is there a way to combine the separate graphs together into 1?

Many thanks :)

enter image description here

1

There are 1 answers

2
TarJae On BEST ANSWER

Here is an example with fake data: The key point is to mutate a new column days and assign it to the x axis with fct_inorder():

library(tidyverse)
library(lubridate)

# Create some fake data:
date <- dmy("6-8-2022"):dmy("5-9-2022")
y = rnorm(31, mean = 2300, sd = 100)
df <- tibble(date, y)

df %>% 
mutate(days = paste0("day",row_number())) %>% 
ggplot(aes(x = fct_inorder(days), y = y, group= 1)) +
  geom_point()+
  geom_line()

enter image description here data:

df <- structure(list(date = 19210:19240, y = c(2379.71407792736, 2349.90296535465, 
2388.14396999868, 2266.84629740315, 2261.95099255488, 2270.90461436351, 
2438.19569234793, 2132.6468717962, 2379.46892613664, 2406.13636097426, 
2176.9392984643, 2219.0521150482, 2221.22674399102, 2399.82972150781, 
2396.76276645913, 2233.62763324748, 2468.98833991591, 2397.47855248058, 
2486.96828322353, 2330.04116860874, 2280.66624489061, 2411.09933781266, 
2281.06682518505, 2281.63162850277, 2235.66952459084, 2271.2152525563, 
2481.86164459452, 2544.25592495568, 2411.90218614317, 2275.60378793237, 
2297.98843827031)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-31L))