How to make multiple graphs in one ggplot

Hi
I want to join several graphs in one plot, but something is wrong with my code, cause R makes only one strange looking graph. Can anybody help?

#Make multiple line plot
df %>%
ggplot(aes(x = Visit, y = LH_concentration, color = Patient, group = 3)) +
geom_line() +
theme_minimal() + ggtitle("Concentration over time") +
scale_color_manual(values = c("red", "blue", "green"))

Could you give us some sample data and a description of what exactly you would like to achieve?

1 Like

It is very hard to be certain without seeing at least some of your data. My first guess is that you should not set group = 3. What are you trying to do with that?
Can you show some of your data with

dput(head(df))
1 Like

My data:

dput(head(df))
structure(list(Visit = c("Visit_1", "Visit_2", "Visit_3", "Visit_1",
"Visit_2", "Visit_3"), Patient = c("Patient_01", "Patient_01",
"Patient_01", "Patient_02", "Patient_02", "Patient_02"), LH_concentration = c("6.6",
"8.8", "7.9", "4.1", "4.5", "4.2")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))

I put group = 3 because I had gotten this error: "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?". Maybe it is something else I have to do in that situation?

I would like to form a graph for each patient based on their three datapoints each, and I would like to show all three graphs in the same plot.

When x consists of categories, I think the default is for each category to be a group
Try this

df %>%
  ggplot(aes(x = Visit, y = LH_concentration, color = Patient, group = Patient)) +
  geom_line() +
  theme_minimal() + ggtitle("Concentration over time") +
  scale_color_manual(values = c("red", "blue", "green"))
1 Like

Thank you so much, it worked! :slight_smile:

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.