1

I have created a set of data frames, their names are:

dude1, dude2, dude3, dude4,......duden (from 1 to n)

These data frames are built from time series made with zoo and my only porpuse is to plot density graphs from them. If I try to plot any of them using gplot It works perfectly, for example for dude5:

ggplot(melt(dude5), aes(value, color=variable)) + geom_density() + xlim(0,30)

enter image description here

but when I try to create a loop from 1 to n to plot them all it doesnt work, how can I create a loop that change part of the name of my variable to plot(dude1, dude2.. etc?

The function paste didnt work for me.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Davido
  • 121
  • 4
  • 12
  • 3
    Its always better to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). – Jaap Feb 05 '17 at 14:45
  • Thanks @Jaap, could you take a look of my last comment and give me your opinion about it, thank you very much!! – Davido Feb 06 '17 at 10:24

1 Answers1

3

You are looking for get()

n <- 10
for(iter in 1:n){
  plotName <- paste0("dude", iter)
  print(ggplot(melt(get(plotName)), aes(value, color=variable)) + geom_density() + xlim(0,30))
}
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • Thank you @Tonio Liebrand, that is definitely what I asked for. However there is something very strange, the command to plot doesnt work in a loop, doesnt show me anything but individual commands to plot do work. Do you know what can I do? – Davido Feb 06 '17 at 10:04
  • This works for me: http://stackoverflow.com/questions/8772630/plotting-during-a-loop-in-rstudio. If not please provide a reproducible example. – Tonio Liebrand Feb 06 '17 at 10:46
  • Good idea @baptiste. I added it. That should answer it completely. – Tonio Liebrand Feb 12 '17 at 23:36