So I am trying to create a line graph which shows scores pre and post intervention for a number of participants. However, the number of participants does not reflect the x axis scale. For example (see picture) the x-axis goes from 2 to 7. But, I want the x axis to only show the participants who completed the questionnaires. e.g. 2,3,5,7. Does anyone know how this can be done? my code is as follows: enter image description here
ggplot(data = my_data, aes(x = Participant))
geom_line(aes(y = PRE_QUIP_RS, colour = "PRE QUIP RS"))
geom_point(aes(y = PRE_QUIP_RS, colour = "PRE QUIP RS"))
geom_line(aes(y = POST_QUIP_RS, colour = "POST QUIP RS"))
geom_point(aes(y = POST_QUIP_RS, colour = "POST QUIP RS"))
scale_colour_manual("",
breaks = c("PRE QUIP RS", "POST QUIP RS"),
values = c("blue", "orange"))
xlab("Participants ")
scale_y_continuous("QUIP RS Scores", limits = c(0,30))
labs(title="Pre and Post QUIP RS Scores")
CodePudding user response:
Coerce Participant
to factor. Factors are internally coded as consecutive integers, so the main question is solved.
As for the repetition of code to plot the several columns, this sort of problem is usually a data reformating problem. See reshaping data.frame from wide to long format.
I have simplified the column names a bit.
suppressPackageStartupMessages({
library(ggplot2)
library(dplyr)
library(tidyr)
})
set.seed(2022)
Participant <- c(2,3,5,7)
PRE <- sample(20:30, 4)
POST <- sample(10:20, 4)
my_data <- data.frame(Participant, PRE, POST)
my_data %>%
pivot_longer(-Participant) %>%
mutate(Participant = factor(Participant)) %>%
ggplot(aes(x = Participant, value, color = name, group = name))
geom_line()
geom_point()
scale_colour_manual("",
breaks = c("PRE", "POST"),
values = c("blue", "orange"))
xlab("Participants")
scale_y_continuous("QUIP RS Scores", limits = c(0,30))
labs(title="Pre and Post QUIP RS Scores")
Created on 2022-09-29 with reprex v2.0.2