how i can plot the multiple condition present in one data using one plot script?












0















I am using R studio to plot boxplot.



My data looks like --



level  Q9HC84       P02768
2 232742108.4 32822363539
2 2886647577 25661800126
1 629319882.2 26745385141
1 1293306218 2173862517
0 632315638.5 994599604.8
0 221394201.3 1659282642


Here is the command I am using to plot a graph:



boxplot(Q9HC84 ~ level ,  data = x , xlab = "Cases", ylab = "Protein",
main = "Q9HC84" , notch = FALSE,
varwidth = TRUE, names = c("C","T","O"))


This shows me a perfect result that is what I want.
But I have 90 proteins name in the data and I want to change this script in a manner that it will generate boxplot for each protein separately.



Same thing i did with lineplot that was easy, but in boxplot it's hard to find how to apply loop. I tried this script ----



for(i in 3:5)
{
x = data.frame(proteomics$level)
y = data.frame(proteomics[i])
boxplot(y ~ x , xlab = "Cases", ylab = "Metabolite",
main = "Q9HC84" , notch = FALSE,
varwidth = TRUE, names = c("Control","Tobacco","OSCC"))

}


can anybody help me ?
Thank you in advance










share|improve this question

























  • I find it hard to see the proteins in your data. Can you point them out?

    – Chris Ruehlemann
    Nov 27 '18 at 6:58











  • Q9HC84 and P02768 are the proteins id . header name of column.

    – sdabral
    Nov 27 '18 at 10:26
















0















I am using R studio to plot boxplot.



My data looks like --



level  Q9HC84       P02768
2 232742108.4 32822363539
2 2886647577 25661800126
1 629319882.2 26745385141
1 1293306218 2173862517
0 632315638.5 994599604.8
0 221394201.3 1659282642


Here is the command I am using to plot a graph:



boxplot(Q9HC84 ~ level ,  data = x , xlab = "Cases", ylab = "Protein",
main = "Q9HC84" , notch = FALSE,
varwidth = TRUE, names = c("C","T","O"))


This shows me a perfect result that is what I want.
But I have 90 proteins name in the data and I want to change this script in a manner that it will generate boxplot for each protein separately.



Same thing i did with lineplot that was easy, but in boxplot it's hard to find how to apply loop. I tried this script ----



for(i in 3:5)
{
x = data.frame(proteomics$level)
y = data.frame(proteomics[i])
boxplot(y ~ x , xlab = "Cases", ylab = "Metabolite",
main = "Q9HC84" , notch = FALSE,
varwidth = TRUE, names = c("Control","Tobacco","OSCC"))

}


can anybody help me ?
Thank you in advance










share|improve this question

























  • I find it hard to see the proteins in your data. Can you point them out?

    – Chris Ruehlemann
    Nov 27 '18 at 6:58











  • Q9HC84 and P02768 are the proteins id . header name of column.

    – sdabral
    Nov 27 '18 at 10:26














0












0








0








I am using R studio to plot boxplot.



My data looks like --



level  Q9HC84       P02768
2 232742108.4 32822363539
2 2886647577 25661800126
1 629319882.2 26745385141
1 1293306218 2173862517
0 632315638.5 994599604.8
0 221394201.3 1659282642


Here is the command I am using to plot a graph:



boxplot(Q9HC84 ~ level ,  data = x , xlab = "Cases", ylab = "Protein",
main = "Q9HC84" , notch = FALSE,
varwidth = TRUE, names = c("C","T","O"))


This shows me a perfect result that is what I want.
But I have 90 proteins name in the data and I want to change this script in a manner that it will generate boxplot for each protein separately.



Same thing i did with lineplot that was easy, but in boxplot it's hard to find how to apply loop. I tried this script ----



for(i in 3:5)
{
x = data.frame(proteomics$level)
y = data.frame(proteomics[i])
boxplot(y ~ x , xlab = "Cases", ylab = "Metabolite",
main = "Q9HC84" , notch = FALSE,
varwidth = TRUE, names = c("Control","Tobacco","OSCC"))

}


can anybody help me ?
Thank you in advance










share|improve this question
















I am using R studio to plot boxplot.



My data looks like --



level  Q9HC84       P02768
2 232742108.4 32822363539
2 2886647577 25661800126
1 629319882.2 26745385141
1 1293306218 2173862517
0 632315638.5 994599604.8
0 221394201.3 1659282642


Here is the command I am using to plot a graph:



boxplot(Q9HC84 ~ level ,  data = x , xlab = "Cases", ylab = "Protein",
main = "Q9HC84" , notch = FALSE,
varwidth = TRUE, names = c("C","T","O"))


This shows me a perfect result that is what I want.
But I have 90 proteins name in the data and I want to change this script in a manner that it will generate boxplot for each protein separately.



Same thing i did with lineplot that was easy, but in boxplot it's hard to find how to apply loop. I tried this script ----



for(i in 3:5)
{
x = data.frame(proteomics$level)
y = data.frame(proteomics[i])
boxplot(y ~ x , xlab = "Cases", ylab = "Metabolite",
main = "Q9HC84" , notch = FALSE,
varwidth = TRUE, names = c("Control","Tobacco","OSCC"))

}


can anybody help me ?
Thank you in advance







r loops boxplot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 6:50









Jon Spring

6,5781726




6,5781726










asked Nov 27 '18 at 6:22









sdabralsdabral

31




31













  • I find it hard to see the proteins in your data. Can you point them out?

    – Chris Ruehlemann
    Nov 27 '18 at 6:58











  • Q9HC84 and P02768 are the proteins id . header name of column.

    – sdabral
    Nov 27 '18 at 10:26



















  • I find it hard to see the proteins in your data. Can you point them out?

    – Chris Ruehlemann
    Nov 27 '18 at 6:58











  • Q9HC84 and P02768 are the proteins id . header name of column.

    – sdabral
    Nov 27 '18 at 10:26

















I find it hard to see the proteins in your data. Can you point them out?

– Chris Ruehlemann
Nov 27 '18 at 6:58





I find it hard to see the proteins in your data. Can you point them out?

– Chris Ruehlemann
Nov 27 '18 at 6:58













Q9HC84 and P02768 are the proteins id . header name of column.

– sdabral
Nov 27 '18 at 10:26





Q9HC84 and P02768 are the proteins id . header name of column.

– sdabral
Nov 27 '18 at 10:26












2 Answers
2






active

oldest

votes


















0














It's difficult to create boxplots with this many groups while still having the plot being readable.



# generate example data
set.seed(1)
np <- 20
level <- rep(1:3, times=c(4, 5, 5))
pnames <- replicate(
np, paste(c(sample(LETTERS, 1), sample(c(0:9, LETTERS), 5)), collapse="")
)
prot <- round(runif(np*length(level), 2213942000, 328223635400), 1)
x <- as.data.frame(cbind(level, matrix(prot, ncol=np)))
colnames(x)[-1] <- pnames


# reshape to long format
library(reshape2)
x.long <- melt(x, id.vars="level")
x.long$level <- c("C","T","O")[x.long$level]

# plot vertically, level + variable
par(xaxs="i", yaxs="i", mar=c(3.5, 3, 0.5, 0.5))
col <- c("orange", "cornflowerblue", "purple")
boxplot(value ~ level + variable, data=x.long, cex=0.5, cex.axis=0.5, las=2,
col=col, border=col, boxwex=0.5)

# plot horizontally, variable + level
par(xaxs="i", yaxs="i", mar=c(2, 3.5, 0.5, 0.5))
col <- rep(c("orange", "cornflowerblue", "purple"),
each=length(unique(x.long$variable)))
boxplot(value ~ variable + level, data=x.long, cex=0.5, cex.axis=0.5, las=1,
horizontal=TRUE, col=col, border=col, boxwex=0.5)


enter image description hereenter image description here






share|improve this answer


























  • Thank you so much . its really working properly for my data sets.

    – sdabral
    Jan 24 at 12:28



















0














Here are 2 solutions:



Assuming your data (slightly simplified) look like this (with three proteins, but that number can obviously be extended):



df <- data.frame(
level = c(2,2,1,1,0,0),
prot1 = c(rnorm(6)+1),
prot2 = c(rnorm(6)),
prot3 = c(rnorm(6)-1)
)


you can re-structure the data in such a way that the protein names form one column and the values they have form another column:



df_new <- data.frame(
level = rep(c(2,2,1,1,0,0),3),
protein_names = c(rep("prot1",6),rep("prot2",6),rep("prot3",6)),
protein_values = c(df$prot1, df$prot2, df$prot3)
)
df_new


The first solution is an R base solution:
This line of code plots all boxplots within a single frame:



boxplot(df_new$protein_values ~ df_new$level+df_new$protein_names)


This for loop plots a separate frame for each protein:



par(mfrow=c(1,3))
for(i in unique(df_new$protein_names)){
boxplot(df_new[df_new$protein_names==i,]$protein_values ~ df_new[df_new$protein_names==i,]$level)
}


The second solution is a ggplot2 solution:



library(ggplot2)

ggplot(df_new, aes(as.factor(level), protein_values)) +
geom_boxplot() +
facet_grid(.~protein_names)


enter image description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53493873%2fhow-i-can-plot-the-multiple-condition-present-in-one-data-using-one-plot-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It's difficult to create boxplots with this many groups while still having the plot being readable.



    # generate example data
    set.seed(1)
    np <- 20
    level <- rep(1:3, times=c(4, 5, 5))
    pnames <- replicate(
    np, paste(c(sample(LETTERS, 1), sample(c(0:9, LETTERS), 5)), collapse="")
    )
    prot <- round(runif(np*length(level), 2213942000, 328223635400), 1)
    x <- as.data.frame(cbind(level, matrix(prot, ncol=np)))
    colnames(x)[-1] <- pnames


    # reshape to long format
    library(reshape2)
    x.long <- melt(x, id.vars="level")
    x.long$level <- c("C","T","O")[x.long$level]

    # plot vertically, level + variable
    par(xaxs="i", yaxs="i", mar=c(3.5, 3, 0.5, 0.5))
    col <- c("orange", "cornflowerblue", "purple")
    boxplot(value ~ level + variable, data=x.long, cex=0.5, cex.axis=0.5, las=2,
    col=col, border=col, boxwex=0.5)

    # plot horizontally, variable + level
    par(xaxs="i", yaxs="i", mar=c(2, 3.5, 0.5, 0.5))
    col <- rep(c("orange", "cornflowerblue", "purple"),
    each=length(unique(x.long$variable)))
    boxplot(value ~ variable + level, data=x.long, cex=0.5, cex.axis=0.5, las=1,
    horizontal=TRUE, col=col, border=col, boxwex=0.5)


    enter image description hereenter image description here






    share|improve this answer


























    • Thank you so much . its really working properly for my data sets.

      – sdabral
      Jan 24 at 12:28
















    0














    It's difficult to create boxplots with this many groups while still having the plot being readable.



    # generate example data
    set.seed(1)
    np <- 20
    level <- rep(1:3, times=c(4, 5, 5))
    pnames <- replicate(
    np, paste(c(sample(LETTERS, 1), sample(c(0:9, LETTERS), 5)), collapse="")
    )
    prot <- round(runif(np*length(level), 2213942000, 328223635400), 1)
    x <- as.data.frame(cbind(level, matrix(prot, ncol=np)))
    colnames(x)[-1] <- pnames


    # reshape to long format
    library(reshape2)
    x.long <- melt(x, id.vars="level")
    x.long$level <- c("C","T","O")[x.long$level]

    # plot vertically, level + variable
    par(xaxs="i", yaxs="i", mar=c(3.5, 3, 0.5, 0.5))
    col <- c("orange", "cornflowerblue", "purple")
    boxplot(value ~ level + variable, data=x.long, cex=0.5, cex.axis=0.5, las=2,
    col=col, border=col, boxwex=0.5)

    # plot horizontally, variable + level
    par(xaxs="i", yaxs="i", mar=c(2, 3.5, 0.5, 0.5))
    col <- rep(c("orange", "cornflowerblue", "purple"),
    each=length(unique(x.long$variable)))
    boxplot(value ~ variable + level, data=x.long, cex=0.5, cex.axis=0.5, las=1,
    horizontal=TRUE, col=col, border=col, boxwex=0.5)


    enter image description hereenter image description here






    share|improve this answer


























    • Thank you so much . its really working properly for my data sets.

      – sdabral
      Jan 24 at 12:28














    0












    0








    0







    It's difficult to create boxplots with this many groups while still having the plot being readable.



    # generate example data
    set.seed(1)
    np <- 20
    level <- rep(1:3, times=c(4, 5, 5))
    pnames <- replicate(
    np, paste(c(sample(LETTERS, 1), sample(c(0:9, LETTERS), 5)), collapse="")
    )
    prot <- round(runif(np*length(level), 2213942000, 328223635400), 1)
    x <- as.data.frame(cbind(level, matrix(prot, ncol=np)))
    colnames(x)[-1] <- pnames


    # reshape to long format
    library(reshape2)
    x.long <- melt(x, id.vars="level")
    x.long$level <- c("C","T","O")[x.long$level]

    # plot vertically, level + variable
    par(xaxs="i", yaxs="i", mar=c(3.5, 3, 0.5, 0.5))
    col <- c("orange", "cornflowerblue", "purple")
    boxplot(value ~ level + variable, data=x.long, cex=0.5, cex.axis=0.5, las=2,
    col=col, border=col, boxwex=0.5)

    # plot horizontally, variable + level
    par(xaxs="i", yaxs="i", mar=c(2, 3.5, 0.5, 0.5))
    col <- rep(c("orange", "cornflowerblue", "purple"),
    each=length(unique(x.long$variable)))
    boxplot(value ~ variable + level, data=x.long, cex=0.5, cex.axis=0.5, las=1,
    horizontal=TRUE, col=col, border=col, boxwex=0.5)


    enter image description hereenter image description here






    share|improve this answer















    It's difficult to create boxplots with this many groups while still having the plot being readable.



    # generate example data
    set.seed(1)
    np <- 20
    level <- rep(1:3, times=c(4, 5, 5))
    pnames <- replicate(
    np, paste(c(sample(LETTERS, 1), sample(c(0:9, LETTERS), 5)), collapse="")
    )
    prot <- round(runif(np*length(level), 2213942000, 328223635400), 1)
    x <- as.data.frame(cbind(level, matrix(prot, ncol=np)))
    colnames(x)[-1] <- pnames


    # reshape to long format
    library(reshape2)
    x.long <- melt(x, id.vars="level")
    x.long$level <- c("C","T","O")[x.long$level]

    # plot vertically, level + variable
    par(xaxs="i", yaxs="i", mar=c(3.5, 3, 0.5, 0.5))
    col <- c("orange", "cornflowerblue", "purple")
    boxplot(value ~ level + variable, data=x.long, cex=0.5, cex.axis=0.5, las=2,
    col=col, border=col, boxwex=0.5)

    # plot horizontally, variable + level
    par(xaxs="i", yaxs="i", mar=c(2, 3.5, 0.5, 0.5))
    col <- rep(c("orange", "cornflowerblue", "purple"),
    each=length(unique(x.long$variable)))
    boxplot(value ~ variable + level, data=x.long, cex=0.5, cex.axis=0.5, las=1,
    horizontal=TRUE, col=col, border=col, boxwex=0.5)


    enter image description hereenter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '18 at 15:42

























    answered Nov 27 '18 at 15:08









    AkselAAkselA

    4,56421325




    4,56421325













    • Thank you so much . its really working properly for my data sets.

      – sdabral
      Jan 24 at 12:28



















    • Thank you so much . its really working properly for my data sets.

      – sdabral
      Jan 24 at 12:28

















    Thank you so much . its really working properly for my data sets.

    – sdabral
    Jan 24 at 12:28





    Thank you so much . its really working properly for my data sets.

    – sdabral
    Jan 24 at 12:28













    0














    Here are 2 solutions:



    Assuming your data (slightly simplified) look like this (with three proteins, but that number can obviously be extended):



    df <- data.frame(
    level = c(2,2,1,1,0,0),
    prot1 = c(rnorm(6)+1),
    prot2 = c(rnorm(6)),
    prot3 = c(rnorm(6)-1)
    )


    you can re-structure the data in such a way that the protein names form one column and the values they have form another column:



    df_new <- data.frame(
    level = rep(c(2,2,1,1,0,0),3),
    protein_names = c(rep("prot1",6),rep("prot2",6),rep("prot3",6)),
    protein_values = c(df$prot1, df$prot2, df$prot3)
    )
    df_new


    The first solution is an R base solution:
    This line of code plots all boxplots within a single frame:



    boxplot(df_new$protein_values ~ df_new$level+df_new$protein_names)


    This for loop plots a separate frame for each protein:



    par(mfrow=c(1,3))
    for(i in unique(df_new$protein_names)){
    boxplot(df_new[df_new$protein_names==i,]$protein_values ~ df_new[df_new$protein_names==i,]$level)
    }


    The second solution is a ggplot2 solution:



    library(ggplot2)

    ggplot(df_new, aes(as.factor(level), protein_values)) +
    geom_boxplot() +
    facet_grid(.~protein_names)


    enter image description here






    share|improve this answer




























      0














      Here are 2 solutions:



      Assuming your data (slightly simplified) look like this (with three proteins, but that number can obviously be extended):



      df <- data.frame(
      level = c(2,2,1,1,0,0),
      prot1 = c(rnorm(6)+1),
      prot2 = c(rnorm(6)),
      prot3 = c(rnorm(6)-1)
      )


      you can re-structure the data in such a way that the protein names form one column and the values they have form another column:



      df_new <- data.frame(
      level = rep(c(2,2,1,1,0,0),3),
      protein_names = c(rep("prot1",6),rep("prot2",6),rep("prot3",6)),
      protein_values = c(df$prot1, df$prot2, df$prot3)
      )
      df_new


      The first solution is an R base solution:
      This line of code plots all boxplots within a single frame:



      boxplot(df_new$protein_values ~ df_new$level+df_new$protein_names)


      This for loop plots a separate frame for each protein:



      par(mfrow=c(1,3))
      for(i in unique(df_new$protein_names)){
      boxplot(df_new[df_new$protein_names==i,]$protein_values ~ df_new[df_new$protein_names==i,]$level)
      }


      The second solution is a ggplot2 solution:



      library(ggplot2)

      ggplot(df_new, aes(as.factor(level), protein_values)) +
      geom_boxplot() +
      facet_grid(.~protein_names)


      enter image description here






      share|improve this answer


























        0












        0








        0







        Here are 2 solutions:



        Assuming your data (slightly simplified) look like this (with three proteins, but that number can obviously be extended):



        df <- data.frame(
        level = c(2,2,1,1,0,0),
        prot1 = c(rnorm(6)+1),
        prot2 = c(rnorm(6)),
        prot3 = c(rnorm(6)-1)
        )


        you can re-structure the data in such a way that the protein names form one column and the values they have form another column:



        df_new <- data.frame(
        level = rep(c(2,2,1,1,0,0),3),
        protein_names = c(rep("prot1",6),rep("prot2",6),rep("prot3",6)),
        protein_values = c(df$prot1, df$prot2, df$prot3)
        )
        df_new


        The first solution is an R base solution:
        This line of code plots all boxplots within a single frame:



        boxplot(df_new$protein_values ~ df_new$level+df_new$protein_names)


        This for loop plots a separate frame for each protein:



        par(mfrow=c(1,3))
        for(i in unique(df_new$protein_names)){
        boxplot(df_new[df_new$protein_names==i,]$protein_values ~ df_new[df_new$protein_names==i,]$level)
        }


        The second solution is a ggplot2 solution:



        library(ggplot2)

        ggplot(df_new, aes(as.factor(level), protein_values)) +
        geom_boxplot() +
        facet_grid(.~protein_names)


        enter image description here






        share|improve this answer













        Here are 2 solutions:



        Assuming your data (slightly simplified) look like this (with three proteins, but that number can obviously be extended):



        df <- data.frame(
        level = c(2,2,1,1,0,0),
        prot1 = c(rnorm(6)+1),
        prot2 = c(rnorm(6)),
        prot3 = c(rnorm(6)-1)
        )


        you can re-structure the data in such a way that the protein names form one column and the values they have form another column:



        df_new <- data.frame(
        level = rep(c(2,2,1,1,0,0),3),
        protein_names = c(rep("prot1",6),rep("prot2",6),rep("prot3",6)),
        protein_values = c(df$prot1, df$prot2, df$prot3)
        )
        df_new


        The first solution is an R base solution:
        This line of code plots all boxplots within a single frame:



        boxplot(df_new$protein_values ~ df_new$level+df_new$protein_names)


        This for loop plots a separate frame for each protein:



        par(mfrow=c(1,3))
        for(i in unique(df_new$protein_names)){
        boxplot(df_new[df_new$protein_names==i,]$protein_values ~ df_new[df_new$protein_names==i,]$level)
        }


        The second solution is a ggplot2 solution:



        library(ggplot2)

        ggplot(df_new, aes(as.factor(level), protein_values)) +
        geom_boxplot() +
        facet_grid(.~protein_names)


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 14:44









        Chris RuehlemannChris Ruehlemann

        46229




        46229






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53493873%2fhow-i-can-plot-the-multiple-condition-present-in-one-data-using-one-plot-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks