Combine multiple plot in loop color each plot by different column in dataframe

I am trying combine the multiple plots according to on of the thread here my problem is I want to separate based on coloured plot with different values can any one help me what I am missing, (Note: my original data i have some 30 column values)

data(iris)
list_data=c("Petal.Length", "Petal.Width")
# Make plots.
plot_list = list()
for (i in list_data) {
  p = ggplot(iris, aes(y=Sepal.Length, x=Sepal.Width)) +
    geom_point(size=3, aes(colour=i))
  plot_list[[i]] = p
}

pdf("plots.pdf")
for (i in list_data) {
  print(plot_list[[i]])
}

dev.off()

>Solution :

We may use .data[[i]] in colour

for (i in list_data) {
  p = ggplot(iris, aes(y=Sepal.Length, x=Sepal.Width)) +
    geom_point(size=3, aes(colour=.data[[i]]))
  plot_list[[i]] = p
}

-output

> plot_list[[1]]

enter image description here

Leave a Reply