yaynikkiprograms
yaynikkiprograms

Reputation: 109

Create graphs by group using ggplot in R

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)

enter image description here

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())
}

Upvotes: 1

Views: 6820

Answers (2)

stefan
stefan

Reputation: 124128

To achieve your desired result

  1. Split your dataframe by group using e.g. split
  2. Use lapply to loop over the list of splitted data frames to create your plots or if you want to add the group labels to the title you could loop over names(df_split).

Note: I converted the id variable to factor. Also, you have to map id on the group aesthetic to get lines per group. However, as your x variable is a numeric there is actually no need for the group aesthetic.

library(ggplot2)

df_split <- split(df, df$group)

lapply(df_split, function(df) {
  ggplot(df, aes(x = x, y = y, group = id, color = factor(id))) +
    geom_line()
})
lapply(names(df_split), function(i) {
  ggplot(df_split[[i]], aes(x = x, y = y, group = id, color = factor(id))) +
    geom_line() +
    labs(title = paste("group =", i))
})
#> [[1]]

#> 
#> [[2]]

And even I if would recommend to use lapply the same could be achieved using a for loop like so:

for (i in names(df_split)) {
  print(
    ggplot(df_split[[i]], aes(x = x, y = y, group = id, color = factor(id))) +
      geom_line() +
      labs(title = paste("group =", i))
  )
}

Upvotes: 1

alexwhitworth
alexwhitworth

Reputation: 4907

Use facet_grid() or facet_wrap()

library(ggplot2)
ggplot(df, aes(x= x, y=y, colour= factor(id))) + geom_line() + facet_grid(group ~ .)

enter image description here

Edit: OP clarifies in comments they want separate graphs, not faceting

# 1
ggplot2(df[df$group == 1,], aes(x= x, y=y, colour= factor(id))) + geom_line()
# 2
ggplot2(df[df$group == 2,], aes(x= x, y=y, colour= factor(id))) + geom_line()

Upvotes: 1

Related Questions