I have to plot the mean points connected by lines of a dataset that include: ID, Pre-treatment, Treatment, Time-point, and the score for each of my sample:
ID Pre-treatment Treatment Time-point Score
-------------------------------------------------------------
A.1 A Control 1 2
A.1 A Heat 1 3
A.1 B Control 1 5
A.1 B Heat 1 4
A.1 C Control 1 7
A.1 C Heat 1 8
A.2 A Control 1 3
A.2 A Heat 1 6
A.2 B Control 1 8
A.2 B Heat 1 6
A.2 C Control 1 9
A.2 C Heat 1 1
A.1 A Control 2 2
A.1 A Heat 2 3
A.1 B Control 2 5
A.1 B Heat 2 4
A.1 C Control 2 7
A.1 C Heat 2 8
A.2 A Control 2 3
A.2 A Heat 2 6
A.2 B Control 2 8
A.2 B Heat 2 6
A.2 C Control 2 9
A.2 C Heat 2 1
....
(the score's values are just random here).
I would like to use ggplot with this d.f and stat_summary to visualize in one single graph the data with : color = Pre-treatment and linetype = Treatment.
Ideally, I should have:
Red and solid line = A_Control; Red and dashed line = A_Heat; Blue and solid line = B_Control; Blue and dashed line = B_Heat; ....
I used this code:
d.f %>%
ggplot( df, mapping = aes(x = Time-point, y = Score,
colour = Pre-Treatment, linetype = Treatment, shape = Treatment))
geom_point()
stat_summary(
aes(group = Pre-Treatment, color = Pre-Treatment),
fun = "mean",
geom = "line",
size = 1)
But it works properly only if I use facet_grid(Treatment ~ .)
. In this case, it separate the the plots in 2 rows. If I don't do it, it group and summarize the A_crontrol A_Heat.
Can you help me? Where is the missing part? Thanks to everyone.
CodePudding user response:
Is this something you are looking for?
#load libraries
library(tibble)
library(reprex)
library(tidyverse)
# dataframe
d.f <- tibble::tribble(
~ID, ~Pre_treatment, ~Treatment, ~Time_point, ~Score,
"A.1", "A", "Control", 1, 2,
"A.1", "A", "Heat" , 1 , 3,
"A.1", "B", "Control", 1 , 5,
"A.1", "B", "Heat" , 1 , 4,
"A.1", "C", "Control", 1 , 7,
"A.1", "C", "Heat" , 1 , 8,
"A.2", "A", "Control", 1 , 3,
"A.2", "A", "Heat" , 1 , 6,
"A.2", "B", "Control", 1 , 8,
"A.2", "B", "Heat" , 1 , 6,
"A.2", "C", "Control", 1 , 9,
"A.2", "C", "Heat" , 1 , 1,
"A.1", "A", "Control", 2 , 2,
"A.1", "A", "Heat" , 2 , 3,
"A.1", "B", "Control", 2 , 5,
"A.1", "B", "Heat" , 2 , 4,
"A.1", "C", "Control", 2 , 7,
"A.1", "C", "Heat" , 2 , 8,
"A.2", "A", "Control", 2 , 3,
"A.2", "A", "Heat" , 2 , 6,
"A.2", "B", "Control", 2 , 8,
"A.2", "B", "Heat" , 2 , 6,
"A.2", "C", "Control", 2 , 9,
"A.2", "C", "Heat" , 2 , 1
)
# graph
d.f %>%
group_by(Time_point, Pre_treatment, Treatment) %>%
mutate(Score_mean = mean(Score)) %>%
ggplot( df, mapping = aes(x = Time_point))
geom_point(aes(y = Score, color = Pre_treatment, shape = Treatment))
geom_line(aes(y = Score_mean, color = Pre_treatment, linetype = Treatment))
labs(x = "Time Point", color = "Pre Treatment")
Created on 2022-08-16 by the reprex package (v2.0.1)