0

I was experimenting with the waffle package in r, and was trying to use a for loop to make multiple plots at once but was not able to get my code to work. I have a dataset with values for each year of renewables,and since it is over 40 years of data, was looking for a simple way to plot these with a for loop rather than manyally year by year. What am I doing wrong? I have it from 1:16 as an experiment to see if it would work, although in reality I would do it for all the years in my dataset.

for(i in 1:16){
renperc<-islren$Value[i]
parts <- c(`Renewable`=(renperc), `Non-Renewable`=100-renperc)
waffle(parts, rows=10, size=1, colors=c("#00CC00", "#A9A9A9"), 
       title="Iceland Primary Energy Supply", 
       xlab=islren$TIME)
}
Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
Maria
  • 1
  • 4
  • What exactly is going wrong? Can you provide some sample data so we can test your code? – Calum You Feb 26 '18 at 23:58
  • Hello :) Sorry for the bad specification. Individual plots worked, but when I tried the for loop (both to make the plots in the same panel or not) no plots actually end up being created. The data is here [link](https://drive.google.com/open?id=1NvRRKY2lIHZSRSXnlseoU5CaZb7zjqxL) – Maria Feb 27 '18 at 07:34

2 Answers2

1

If I get your question correctly you want to plot all the 16 iterations in a same panel? You can parametrise your plot window to be divided into 16 smaller plots using par(mfrow = c(4,4)) (creating a 4 by 4 matrix and plotting into each cells recursively).

## Setting the graphical parameters
par(mfrow = c(4,4))

## Running the loop normally
for(i in 1:16){
    renperc<-islren$Value[i]
    parts <- c(`Renewable`=(renperc), `Non-Renewable`=100-renperc)
    waffle(parts, rows=10, size=1, colors=c("#00CC00", "#A9A9A9"), 
           title="Iceland Primary Energy Supply", 
           xlab=islren$TIME)
}

If you need more plots (e.g. 40) you can increase the numbers in the graphical parameters (e.g. par(mfrow = c(6,7))) but that will create really tiny plots. One solution is to do it in multiple loops (for(i in 1:16); for(i in 17:32); etc.)

Thomas Guillerme
  • 1,747
  • 4
  • 16
  • 23
  • Hello and thanks for the help :) I tried multiple plots in one panel and just individual ines, but my plots don't show up - so I imagied something was going wrong with my loop. It still does not plot them, and I can't figure out why. Thank you for the answer anyway, and I'll also link the [data](https://drive.google.com/open?id=1NvRRKY2lIHZSRSXnlseoU5CaZb7zjqxL) if you'd like to take a look – Maria Feb 27 '18 at 07:38
0

UPDATE: The code simply wasn't plotting anything when i tried putting in anything above one value (ex. 1:16) or a letter, both in terms of separate plots or many in one plot window (which I think perhaps waffle does not support in the same way as regular plots). In the end, I managed by making it into a function, although I'm still not sure why my original method wouldn't work if this did. See the code that worked below. I also tweaked it a bit, adding ggsave for example.

    #function
    waffling <- function(x){
    renperc<-islren$Value[x]
    parts <- c(`Renewable`=(renperc), `Non-Renewable`=100-renperc)
    waffle(parts, rows=10, size=1, colors=c("#00CC00", "#A9A9A9"), title="", 
    xlab=islren$TIME[x])
    ggsave(file=paste0("plot_", x,".png"))}

    for(i in 1:57){
    waffling(i)
    }
Maria
  • 1
  • 4