Lucky sardar
Lucky sardar

Reputation: 151

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

Upvotes: 2

Views: 44

Answers (1)

akrun
akrun

Reputation: 887118

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

Upvotes: 2

Related Questions