boxplot with a for loop
2
0
Entering edit mode
9.5 years ago
yasjas ▴ 70

Hi all,

I tried to make a boxplot for each classes of (data) based on repeat names (rep_name) with a for loop but it didn't give me the desired result. What it gave me was the last boxplot of the last class

Any idea on how I can have a boxplot for each class with a loop instead of doing it manually?

Thanks for any answer

for (i in length(data)){
    boxplot(data[[i]][5:8])
}

data

[[1]]
    rep_name   rep_family    gene       distance Hepatocytes_B1 Hepatocytes_B3  Huh.7_B1  Huh.7_B2
  LTR18B        LTR           NDUFAB1      572         9.3038          9.287                     10.9524    10.8579

[[2]]
     rep_name       rep_family   gene distance Hepatocytes_B1 Hepatocytes_B3 Huh.7_B1 Huh.7_B2
        LTR19B        LTR          CASP10        0         6.5334         5.3218             7.1686              5.9633
        LTR19B        LTR               CD38        0         5.8011         6.9873              5.7066             2.5126

[[3]]
      rep_name     rep_family  gene      distance Hepatocytes_B1 Hepatocytes_B3  Huh.7_B1     Huh.7_B2
     LTR4                 LTR         ACSM3        0          8.744         6.9615                      5.9441            3.1032

[[4]]
      rep_name     rep_family    gene    distance Hepatocytes_B1 Hepatocytes_B3    Huh.7_B1    Huh.7_B2
     LTR53                  LTR       TNMD    66637         0.5905         0.9610                     0.6130        0.7612
    LTR53                    LTR      MTMR7        0         3.9547         3.7264                        3.5522        4.2649
    LTR53                    LTR        VPS41     2226         7.1890         7.2522                    7.1952        7.0576
R • 15k views
ADD COMMENT
2
Entering edit mode
9.5 years ago

Rechecking the code I think that the correct version is (using pdf export as suggested by Sam):

pdf("Image.pdf")
for(i in 1:length(data)){
  set = data[[i]]
  boxplot(as.numeric(unlist(set[5:8])))
}
dev.off()
ADD COMMENT
1
Entering edit mode
9.5 years ago
Sam ★ 4.8k

You can either use par, see tutorial here

Or I do prefer just open a pdf and output images there

pdf("Image.pdf")
for (i in 1:length(data)){
    boxplot(data[[i]][5:8])
}
dev.off()
ADD COMMENT
2
Entering edit mode

The for cycle must be:

for (i in 1:length(data)){
ADD REPLY
1
Entering edit mode

Oh, ya, I forgot to check his code. That also explain why he will only ever see the last boxplot.

ADD REPLY
1
Entering edit mode

Further explanation on graphing in R:

When you call boxplot() (or any graphing function) in R, it draws it in a default graphic device, which it closes after you're done. Every time you call another boxplot() function, it overwrites your previous plot. What Sam suggests above is opening your own graphics device manually (in this case, a pdf), write what output you want to it, and then close it.

A couple of things to keep in mind: be sure to close the graphic device explicitly with dev.off(), and be careful with using loops to write files, since if your loop is incorrect and turns out to be an infinite loop, you will create a huge file and potentially cause issues.

ADD REPLY
0
Entering edit mode

Thank you guys, it is more clear now and thanks for the link of the tutorial

ADD REPLY

Login before adding your answer.

Traffic: 1083 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6