Reputation: 11
So I have a dataset for which I'm planning to create several graph through a loop using the unique ID of each set, here you'll able to see the how the graph looks for an single individual.
library(ggplot2)
RID <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
COMP <- c(0, 2, 1, 0, 1, 3, 0, 1, 3, 3, 2, 1, 3, 0, 1)
TIME <- c(0.01, 0.03, 0.03, 0.05, 0.06, 0,07, 0.07, 0.07, 0.08, 0.11, 0.11, 0.15, 0.16, 0.19)
NDV <- c(23, 34, 54, 11, 23, 76, 45, 38, 22, 45, 98, 19, 10, 11, 56)
df <- data.frame(RID, COMP, TIME, NDV)
I created a function to graph the time course of all this datapoints (I've done this because the I'm planning to create multiple graphs and put that into a loop) with 2 axis atm.
graph_fx_ex <- function(df) {
mycolors <- c("C1" = "blue", "C0" = "red", "C2" = "green", "C3" = "yellow", "Time (h)" = "black")
ggplot(df, aes(x = TIME, y = NDV, group = as.factor(COMP), color = as.factor(COMP))) +
labs(title = paste("RID = ", df$RID[1])) +
geom_path() +
geom_point() +
scale_y_continuous(name = "comp 1 value", sec.axis = sec_axis(~ 1*., name = "comp 2 value")) +
scale_x_continuous(name = "Time (h)") +
scale_color_manual(name = "case", values = c("blue", "red", "green", "yellow")) +
theme(
axis.title = element_text(size = 10, face = "bold"),
axis.title.y = element_text(color = mycolors["C1"]),
axis.text.y = element_text(color = mycolors["C1"]),
axis.title.y.right = element_text(color = mycolors["C0"]),
axis.text.y.right = element_text(color = mycolors["C0"])
)
}
when I resolve this for my dataset there's only 2 Y axis. however since I have 4 unique components I'd like to know how can I add more. I've read all the GGPLOT2 regarding multiple axis and nowhere have I found where to add parallel multiple Y axis.
graph_fx_ex(df)
I've used a long format for the class column (COMP) since the points don't share the actual times. How can I add more parallel axis to the graph to also include the other 2 classes?
Thanks :)
IMPORTANT PIECE OF INFORMATION: COMP isn't multiple measurements of the same thing per ID, but distinct measurements (meaning just a single scale isn't possible). While COMP = 0 might codify for something like body temperature, COMP = 1 would be something like blood pressure. These are different biomarkers monitored per patients and the rationale behind each is so that clinicians can compare how they evolve over time, hence the need for unique scales for each COMP case.
Upvotes: 0
Views: 50