0

I got a dataset that looks like this

a    b    c    d
x    1.1  lol  xyz
x    1.4  lal  xyw
y    1.2  lel  xyz
x    1.4  lol  xyw
y    1.8  lel  xyz

I get my plot with ggplot(filter(df, c=="lol"), aes(x=b)) + geom_bar() + facet_wrap( ~ d) + xlab("xlab") + ylab ("ylab"). I am trying to plot this for every c factor with a for loop, but I am not sure how to do this.

murpholinox
  • 639
  • 1
  • 4
  • 14

1 Answers1

1
plot_list = list()
for(uniq_c in unique(df$c)) {
  plot_list[[uniq_c]] = ggplot(filter(df, c == uniq_c), aes(x = b)) +
    geom_bar() +
    facet_wrap( ~ d) +
    xlab("xlab") + ylab ("ylab")
}

print(plot_list[["lal"]]) # print a single plot
lapply(plot_list, print)  # print all plots
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294