Bella_18
Bella_18

Reputation: 632

Plotting with different colors in r

I have an i,j looped data.

I want to plot and save different pdfs for all i's where each pdf has a lines plot of j with different colors of j's (10 pdf files of i's with each pdf have 3 line plots(j=3))

Code I used for plot

for(i in 1:10){
#opening pdf 
pdf(paste0("plot of_",i,".pdf")
for(j in 1:3){
       cl <- rainbow(3)
        #my_data is calculated here with the hep of i and j

    if(j== 1){
             #plotiing a null plot so that i can plot lines data for j = 1,2 and 3
              plot(NULL, xlim=c(0,10), ylim=c(0,max(my_data)), 
                      ylab="y label", xlab="x label")
              lines(my_data, type="l", col=cl[j], lwd = 2)
             }
         else{
            lines(my_data, type="l", col=cl[j], lwd = 2)
             }
            }
    dev.off()
 }

But my code doesn't work. I hope my question is clear. Any other approaches would be appreciated

Upvotes: 0

Views: 93

Answers (1)

dcarlson
dcarlson

Reputation: 11046

Maybe this will help you clarify your question. Using the iris data available on R, I created the following reproducible data frame using dput(flowers). Just copy/paste the code into the R console:

flowers <- structure(list(Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("setosa", 
"versicolor", "virginica"), class = "factor"), Sepal.Length = c(4.3, 
4.59, 4.7, 4.8, 4.96, 5, 5.1, 5.1, 5.32, 5.41, 5.8, 4.9, 5.38, 
5.5, 5.6, 5.7, 5.9, 6.04, 6.2, 6.4, 6.7, 7, 4.9, 5.8, 6.1, 6.3, 
6.4, 6.5, 6.7, 6.83, 7.2, 7.61, 7.9), Sepal.Width = c(2.3, 3, 
3.1, 3.2, 3.4, 3.4, 3.5, 3.6, 3.72, 3.9, 4.4, 2, 2.3, 2.5, 2.6, 
2.7, 2.8, 2.9, 3, 3, 3.11, 3.4, 2.2, 2.59, 2.7, 2.8, 2.9, 3, 
3, 3.1, 3.2, 3.31, 3.8), Petal.Length = c(1, 1.3, 1.3, 1.4, 1.4, 
1.5, 1.5, 1.5, 1.6, 1.7, 1.9, 3, 3.59, 3.9, 4, 4.2, 4.35, 4.5, 
4.5, 4.7, 4.8, 5.1, 4.5, 4.9, 5.1, 5.1, 5.36, 5.55, 5.6, 5.8, 
6, 6.31, 6.9), Petal.Width = c(0.1, 0.19, 0.2, 0.2, 0.2, 0.2, 
0.2, 0.3, 0.3, 0.4, 0.6, 1, 1, 1.18, 1.27, 1.3, 1.3, 1.4, 1.43, 
1.5, 1.51, 1.8, 1.4, 1.79, 1.8, 1.8, 1.9, 2, 2.1, 2.2, 2.3, 2.4, 
2.5)), row.names = c(NA, -33L), class = "data.frame")
str(flowers)
'data.frame':   33 obs. of  5 variables:
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Sepal.Length: num  4.3 4.59 4.7 4.8 4.96 5 5.1 5.1 5.32 5.41 ...
 $ Sepal.Width : num  2.3 3 3.1 3.2 3.4 3.4 3.5 3.6 3.72 3.9 ...
 $ Petal.Length: num  1 1.3 1.3 1.4 1.4 1.5 1.5 1.5 1.6 1.7 ...
 $ Petal.Width : num  0.1 0.19 0.2 0.2 0.2 0.2 0.2 0.3 0.3 0.4 ...

A simple way to subset a data frame is to use split() to create a list of data frames:

spp <- levels(flowers$Species)
flowers.sp <- split(flowers, flowers$Species)
clrs <- rainbow(3)

Now we just need a single loop:

for (i in 1:length(spp)) {
    pdf(paste0("Iris", spp[i], ".pdf"))
    matplot(flowers.sp[[i]][, 2], flowers.sp[[i]][, 3:5], type="l", lty=1, lwd=2, col=clrs, 
         main=paste("Iris", spp[i]), xlab="Sepal.Length", ylab="Measures")
    dev.off()
}

This creates 3 pdf files with the plots for the first Sepal.Length against each of the other three variables. For example, the first plot:

Iris Plot

Upvotes: 1

Related Questions