0

I am trying to plot 18 individual plots on a 3x6 multiplot in R. To be more efficient I have created these plots as a loop, however I would like the plots in each column to have their own color (i.e. the all the plots in column 1 would be red, all the plots in column 2 would be blue etc.). Is there a way I can do this while still retaining loop format?

par(mfcol = c(3,6))
for(i in 1:6)
{
  plot(sigma_trace[,i], type ='l', main = paste("Sigma Traceplot Chain", i))
  plot(theta_1_trace[,i], type = 'l', main = paste("Theta[1] Traceplot Chain", i))
  plot(theta_2_trace[,i], type = 'l', main = paste("Theta[2] Traceplot Chain", i))
}

So basically, I think I want each loop statement to follow the same pattern of colours. Is this possible?

Thanks.

2 Answers2

0

You can make a colour palette using RColorBrewer and then call each colour in your loop. For example.

library(RColorBrewer)

# set the colour palette
cols <- brewer.pal(4,'Set2')

# variables to plot
x = (1:250)/10
y = cos(x)

# plot in the loop
op <- par(mfrow = c(2, 2))
for (i in 1:4){
  plot(x, y, col=cols[i], type='l', lwd=3)
}
par(op)

enter image description here

Here's an overview of the package.

Muon
  • 1,294
  • 1
  • 9
  • 31
0

In Base R you can use colorRampPalette() to create gradient, or you can even just make an object with the colours that you wan to reference:

plotcolors <- colorRampPalette(c("gold","blue"))(6)
par(mfrow = c(2, 3))

for(i in 1:6){
  plot(1:10,1:10,type='l',col=plotcolors[i])
}

If you want to specify all 6 of your colours its as easy as modifying the above code

plotcolors <- c("red","blue","green","black","yellow","purple")

Custom Colour Ramp

Daniel O
  • 4,258
  • 6
  • 20