I'm relatively new to using ggplot2
in R and have been struggling with this for awhile. I have figured out how to get everything from one data frame on a graph (that is pretty easy...), and how to write a loop function to get each observation (id
in the example below) onto their own graphs but not how to create separate graphs with multiple id
per group
, when the id
and group
can change each time I run the code. Here is some sample data and the output I am trying to produce.
x <- c(1,3,6,12,24,48,72,1,3,6,12,24,48,72,1,3,6,12,24,48,72,1,3,6,12,24,48,72)
y <- c(8,27,67,193,271,294,300,10,30,70,195,280,300,310,5,25,60,185,250,275,300,15,40,80,225,275,325,330)
group <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
id <- c(100,100,100,100,100,100,100,101,101,101,101,101,101,101,102,102,102,102,102,102,102,103,103,103,103,103,103,103)
df <- data.frame(x,y,group,id)
Similar questions were asked here and here but I still can't figure out how to do what I need because I need separate graphs (not facets) by group
with multiple id
on the same graph.
Edit to add attempt -
l <- unique(df$group)
for(l in df$group){
print(ggplot(df, aes(x = x, y = y, group = group, color = id))+
geom_line())
}