Home Forum Statistica con R creare boxplot raggruppati

This topic contains 4 replies and has 2 voices.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #4864

    giupar93
    Participant

    Buon pomeriggio, ho un dataframe in questo modo

         X__2 A           B            C
      Medium       <NA> -0.36930000000000002               -0.3372
      Medium       <NA> -0.37780000000000002  -0.25619999999999998
      Medium       <NA>              -0.2863               -0.2039
      Medium       <NA> -0.34870000000000001  -0.27129999999999999
      Medium       <NA>              -0.1875 4.4499999999999998E-2
      Medium       <NA>              -0.4279               -0.1807
      Medium       <NA>              -0.3679  -0.34910000000000002
      Medium       <NA> -0.33450000000000002  -0.20680000000000001
      Medium       <NA> -0.17269999999999999  -0.23860000000000001
     Medium       <NA> -0.27250000000000002  -0.25769999999999998
     Medium       <NA> -0.42570000000000002  -0.38450000000000001
        Up       <NA> -0.36770000000000003 -0.28620000000000001
        Up       <NA> -0.35820000000000002 -0.34760000000000002
        Up       <NA> -0.36720000000000003              -0.3211
        Up       <NA> -0.24410000000000001              -0.1409
        Up       <NA> -0.28070000000000001              -0.2238
        Up       <NA> -0.21440000000000001              -0.2104
        Up       <NA> -0.29699999999999999              -0.2198
        Up       <NA> -0.39300000000000002                -0.42
        Up       <NA> -0.26690000000000003 -0.33760000000000001
        Up       <NA> -0.44829999999999998 -0.30099999999999999

    vorrei creare un boxplot di A B C raggruppati per UP e MEDIUM..è possibile fare ciò?

    grazie mille!

    #4865

    Ciao giupar!
    Ti propongo questa soluzione:

    
    library(ggplot2)
    library(tidyr)
     
     
    # creo dataset
    group <- c(rep("medium", 5), rep("Up", 7))
    alpha <- c(1,2,3,4,2,2,5,4,3,7,8,9)
    beta <- c(11,34,56,21,12,11,1,2,3,4,5,6)
    charlie <- c(5,3,4,5,6,1,7,8,4,5,6,7)
    (data <- data.frame(group, alpha, beta, charlie))
    
    group alpha beta charlie
    1  medium     1   11       5
    2  medium     2   34       3
    3  medium     3   56       4
    4  medium     4   21       5
    5  medium     2   12       6
    6      Up     2   11       1
    7      Up     5    1       7
    8      Up     4    2       8
    9      Up     3    3       4
    10     Up     7    4       5
    11     Up     8    5       6
    12     Up     9    6       7
     
    # trasformo il dataset in formato long in modo che ggplot2 lo possa utilizzare
    (data <- gather(data, var, value, -group))
        group     var value
    1  medium   alpha     1
    2  medium   alpha     2
    3  medium   alpha     3
    4  medium   alpha     4
    5  medium   alpha     2
    6      Up   alpha     2
    7      Up   alpha     5
    8      Up   alpha     4
    9      Up   alpha     3
    10     Up   alpha     7
    11     Up   alpha     8
    12     Up   alpha     9
    13 medium    beta    11
    14 medium    beta    34
    15 medium    beta    56
    16 medium    beta    21
    17 medium    beta    12
    18     Up    beta    11
    19     Up    beta     1
    20     Up    beta     2
    21     Up    beta     3
    22     Up    beta     4
    23     Up    beta     5
    24     Up    beta     6
    25 medium charlie     5
    26 medium charlie     3
    27 medium charlie     4
    28 medium charlie     5
    29 medium charlie     6
    30     Up charlie     1
    31     Up charlie     7
    32     Up charlie     8
    33     Up charlie     4
    34     Up charlie     5
    35     Up charlie     6
    36     Up charlie     7
    > 
    > (plot <- ggplot(data, aes(x=var, y=value, fill = group)) + 
    +   geom_boxplot())
    

    boxplots raggruppati

    #4868

    giupar93
    Participant

    GRAZIE MILLE!!!

    #4869

    giupar93
    Participant

    domanda off topic, ma se volessi creare un boxplot per più colonne e salvare ogni volta il plot..è possibile farlo con il ciclo for?

    #4870

    giupar93
    Participant

    Vorrei riprodurre il primo esempio esempio della soluzione data qui https://stackoverflow.com/questions/14604439/plot-multiple-boxplot-in-one-graph

    ma non riesco a capire come mai ottengo delle linee tratteggiate

    https://s9.postimg.org/7udqrydzz/Schermata_a_2017-07-10_11-50-53.png

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.