banner



How To Add Data Labels To Boxplot In R


Yesterday I wanted to create a box-plot for a small dataset to see the evolution of iii stations through a 3 days period. I similar box-plots very much because I think they are one of the clearest ways of showing trend in your data. R is extremely expert for this type of plot and, for this reason, I decided to add together a post on my blog to show how to create a box-plot, but also because I want to use my ain weblog to help me recollect pieces of code that I might want to employ in the future simply that I tend to forget.

For this example I first created a dummy dataset using the function rnorm() which generates random normal-distributed sequences. This office requires 3 arguments, the number of samples to create, the hateful and the standard deviation of the distribution, for example:

rnorm ( n = 100 , mean = iii , sd = i )

This generates 100 numbers (floats to exist exact), which accept mean equal to 3 and standard deviation equal to 1.

To generate my dataset I used the following line of code:

information <- data.frame ( Stat11 = rnorm ( 100 , hateful = 3 , sd = 2 ) ,

Stat21 = rnorm ( 100 , mean = 4 , sd = ane ) ,

Stat31 = rnorm ( 100 , mean = 6 , sd = 0.5 ) ,

Stat41 = rnorm ( 100 , mean = 10 , sd = 0.5 ) ,

Stat12 = rnorm ( 100 , mean = 4 , sd = 2 ) ,

Stat22 = rnorm ( 100 , hateful = 4.5 , sd = 2 ) ,

Stat32 = rnorm ( 100 , mean = 7 , sd = 0.v ) ,

Stat42 = rnorm ( 100 , mean = viii , sd = 3 ) ,

Stat13 = rnorm ( 100 , hateful = 6 , sd = 0.5 ) ,

Stat23 = rnorm ( 100 , mean = 5 , sd = 3 ) ,

Stat33 = rnorm ( 100 , mean = eight , sd = 0.2 ) ,

Stat43 = rnorm ( 100 , mean = 4 , sd = 4 ))

This line creates a data.frame with 12 columns that looks like this:



Stat11

Stat21

Stat31

Stat41

Stat12

Stat22

Stat32

Stat42

Stat13

Stat23

Stat33

Stat43

5

ii

9

-3

10

4

1

1

4

1

five

9

6

thirteen

eight

3

seven

iii

10

ten

10

5

nine

eight

4

iv

half dozen

0

10

6

7

6

6

8

2

7

6

7

6

3

9

1

7

0

1

0

vi

0

0

2

8

i

vi

viii

0

8

iii

x

ix

8

0

xix

10

0

11

10

5

6

5

eight

10

1

7

4

5

-five

7

0

three

5

2

5

5

3

4

12

9

-four

7

1

ix

0

7

two

1

7

7

iii

9

0

xi

0

8

ane

seven

0

7

seven

six

nineteen

8

3

10

10

9

6

0

2

8

2

6

thirteen

half-dozen

-five

12

viii

1

4

0

4

five

10

8

eleven

6

-1

11

4

4

i

4

half dozen

half-dozen

x

8

13

5

-five

seven

10

0

iv

2

seven

3

1

ii

8

five

-2

5

seven

4

two

vii

0

iii

one

8

11

7

three

11

1

0

9

2

3

five

8

iv

nineteen

5

-1

11

half dozen

3

4

ix

5

9

0

2

9

5

-3

12

7

half-dozen

iv

eight

2

6

8

7

x

5

-4

8

9

6

9

1

four

3

four




As I mentioned before, this should stand for iv stations for which the measure were replicated in iii successive days.

Now, for the creation of the box-plot the simplest function is boxplot() and tin be simply called by adding the name of the dataset every bit simply argument:

boxplot ( data )

This creates the post-obit plot:

 It is already a good plot, but it needs some adjustments. Information technology is in blackness and white, the box-plots are evenly spaced, fifty-fifty though they are from iii unlike replicates, there are no labels on the centrality and the names of the stations are not all reported.

And then now we need to start doing some tweaking.

Showtime, I desire to draw the names of the stations vertically, instead of horizontally. This tin can be hands done with the statement las. So now the call to the function boxplot()becomes:

boxplot ( data , las = 2 )

This generates the post-obit plot:

Next, I want to modify the proper noun of the stations so that they expect less confusing. For doing that I can use the option names:

boxplot ( data , las = 2 , names = c ( "Station 1" , "Station two" , "Station iii" , "Station 4" , "Station 1" , "Station ii" , "Station three" , "Station 4" , "Station ane" , "Station two" , "Station 3" , "Station 4" ))

which generates this plot:



If the names are likewise long and they do non fit into the plot's window y'all can increase information technology past using the option par:

boxplot ( data , las = 2 , par ( mar = c ( 12 , 5 , four , ii ) + 0.one ) , names = c ( "Station one" , "Station 2" , "Station 3" , "Station 4" , "Station 1" , "Station two" , "Station 3" , "Station 4" , "Station 1" , "Station 2" , "Station 3" , "Station four" ))


At present I want to group the 4 stations so that the division in 3 successive days is clearer. To do that I tin use the choice at, which let me specify the position, forth the X axis, of each box-plot:

boxplot ( information , las = 2 , at = c ( one , 2 , 3 , iv , six , vii , 8 , nine , eleven , 12 , xiii , fourteen ) , par ( mar = c ( 12 , 5 , 4 , 2 ) + 0.1 ) , names = c ( "Station 1" , "Station 2" , "Station 3" , "Station 4" , "Station 1" , "Station two" , "Station 3" , "Station 4" , "Station one" , "Station two" , "Station 3" , "Station four" ))

Hither I am specifying that I want the first four box-plots at position x=ane, ten=2, x=three and x=4, then I want to leave a space between the fourth and the fifth and place this concluding at x=6, then on.

If you lot want to add colours to your box plot, yous can use the option col and specify a vector with the color numbers or the colour names. Yous can find the colour numbers here, and the color names here.

Here is an example:

boxplot ( data , las = 2 , col = c ( "blood-red" , "sienna" , "palevioletred1" , "royalblue2" , "red" , "sienna" , "palevioletred1" ,

"royalblue2" , "red" , "sienna" , "palevioletred1" , "royalblue2" ) ,

at = c ( ane , 2 , 3 , four , half-dozen , 7 , 8 , nine , 11 , 12 , xiii , 14 ) , par ( mar = c ( 12 , v , 4 , 2 ) + 0.1 ) ,

names = c ( "Station 1" , "Station 2" , "Station 3" , "Station 4" , "Station one" , "Station two" , "Station 3" , "Station four" , "Station ane" , "Station ii" , "Station 3" , "Station iv" ))



Now, for the finishing touches, we can put some labels to plot.

The common way to put labels on the axes of a plot is by using the arguments xlab and ylab.

Allow'southward attempt it:


boxplot ( data , ylab = "Oxigen (%)" , xlab = "Time" , las = ii , col = c ( "blood-red" , "sienna" , "palevioletred1" , "royalblue2" , "red" , "sienna" , "palevioletred1" , "royalblue2" , "cerise" , "sienna" , "palevioletred1" , "royalblue2" ) ,at = c ( 1 , 2 , three , 4 , six , seven , eight , 9 , 11 , 12 , 13 , 14 ) , par ( mar = c ( 12 , five , 4 , 2 ) + 0.1 ) , names = c ( "Station 1" , "Station ii" , "Station iii" , "Station 4" , "Station 1" , "Station ii" , "Station 3" , "Station four" , "Station 1" , "Station ii" , "Station 3" , "Station 4" ))

I just added the two arguments highlighted, but the result is not what I was expecting

As you tin can meet from the epitome above, the label on the Y centrality is identify very well and nosotros can go along it. On the other hand, the characterization on the X axis is drawn right beneath the stations names and information technology does not look good.

To solve this is better to delete the pick xlab from the boxplot call and instead use an boosted role called mtext(), that places a text outside the plot area, but within the plot window. To place text within the plot expanse (where the box-plots are actually depicted) you lot need to use the function text().

The function mtext() requires three arguments: the label, the position and the line number.

An example of a call to the function mtext is the following:

mtext("Label", side = 1, line = seven)

the option side takes an integer between one and four, with these meaning: 1=bottom, two=left, 3=top, 4=right

The choice line takes an integer with the line number, starting from 0 (which is the line closer to the plot axis). In this instance I put the characterization onto the 7thursday line from the X axis.

With these pick you can produce box plot for every situation.

The following is just one example:

This is the script:

data <- information.frame ( Stat11 = rnorm ( 100 , hateful = 3 , sd = 2 ) , Stat21 = rnorm ( 100 , mean = four , sd = 1 ) , Stat31 = rnorm ( 100 , hateful = vi , sd = 0.v ) , Stat41 = rnorm ( 100 , mean = 10 , sd = 0.5 ) , Stat12 = rnorm ( 100 , mean = 4 , sd = two ) , Stat22 = rnorm ( 100 , mean = 4.5 , sd = 2 ) , Stat32 = rnorm ( 100 , hateful = vii , sd = 0.5 ) , Stat42 = rnorm ( 100 , hateful = 8 , sd = 3 ) , Stat13 = rnorm ( 100 , mean = half-dozen , sd = 0.5 ) , Stat23 = rnorm ( 100 , mean = five , sd = 3 ) , Stat33 = rnorm ( 100 , mean = 8 , sd = 0.two ) , Stat43 = rnorm ( 100 , mean = four , sd = 4 )) boxplot ( data , las = 2 , col = c ( "red" , "sienna" , "palevioletred1" , "royalblue2" , "scarlet" , "sienna" , "palevioletred1" , "royalblue2" , "red" , "sienna" , "palevioletred1" , "royalblue2" ) , at = c ( one , ii , 3 , 4 , 6 , 7 , 8 , nine , 11 , 12 , 13 , 14 ) , par ( mar = c ( 12 , five , 4 , 2 ) + 0.i ) , names = c ( "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" , "" ) , ylim = c (- 6 , 18 )) #Station labels mtext ( "Station1" , side = 1 , line = i , at = i , las = 2 , font = 1 , col = "red" ) mtext ( "Station2" , side = 1 , line = 1 , at = 2 , las = 2 , font = 2 , col = "sienna" ) mtext ( "Station3" , side = 1 , line = 1 , at = 3 , las = 2 , font = 3 , col = "palevioletred1" ) mtext ( "Station4" , side = 1 , line = 1 , at = 4 , las = 2 , font = 4 , col = "royalblue2" ) mtext ( "Station1" , side = 1 , line = 1 , at = 6 , las = 2 , font = i , col = "red" ) mtext ( "Station2" , side = i , line = 1 , at = 7 , las = 2 , font = 2 , col = "sienna" ) mtext ( "Station3" , side = one , line = 1 , at = viii , las = 2 , font = iii , col = "palevioletred1" ) mtext ( "Station4" , side = 1 , line = 1 , at = 9 , las = 2 , font = iv , col = "royalblue2" ) mtext ( "Station1" , side = i , line = 1 , at = eleven , las = two , font = i , col = "red" ) mtext ( "Station2" , side = 1 , line = 1 , at = 12 , las = 2 , font = 2 , col = "sienna" ) mtext ( "Station3" , side = 1 , line = 1 , at = 13 , las = 2 , font = 3 , col = "palevioletred1" ) mtext ( "Station4" , side = 1 , line = one , at = fourteen , las = two , font = 4 , col = "royalblue2" ) #Centrality labels mtext ( "Time" , side = 1 , line = half dozen , cex = two , font = 3 ) mtext ( "Oxigen (%)" , side = 2 , line = 3 , cex = 2 , font = 3 ) #In-plot labels text ( i , 4 , "*" ) text ( 6 , iv , "*" ) text ( 11 , 4 , "*" ) text ( 2 , 9 , "A" , cex = 0.8 , font = iii ) text ( 7 , 11 , "A" , cex = 0.eight , font = 3 ) text ( 12 , 15 , "A" , cex = 0.viii , font = 3 )



How To Add Data Labels To Boxplot In R,

Source: https://www.r-bloggers.com/2013/06/box-plot-with-r-tutorial/

Posted by: moodybeftedind1982.blogspot.com

0 Response to "How To Add Data Labels To Boxplot In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel