Boxplot in R – R Boxplot

Daten erzeugen

data<-data.frame(Stat11=rnorm(100,mean=3,sd=2),
Stat21=rnorm(100,mean=4,sd=1),
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,mean=4.5,sd=2),
Stat32=rnorm(100,mean=7,sd=0.5),
Stat42=rnorm(100,mean=8,sd=3),
Stat13=rnorm(100,mean=6,sd=0.5),
Stat23=rnorm(100,mean=5,sd=3),
Stat33=rnorm(100,mean=8,sd=0.2),
Stat43=rnorm(100,mean=4,sd=4))

Ergibt die Datentabelle

Stat11Stat21Stat31Stat41Stat12Stat22Stat32Stat42Stat13Stat23Stat33Stat43
529-3104114159
6138373101010598
4460106766827
676391701060
0281680831098
01910011105658101
745-570352553
4129-471907217
7390110817077
619831010960282
6136-51281404510
8116-11144146610
8135-5710042731
285-257427031
81173111092358
4195-1116349590
295-3127648268
7105-489691434

Boxplot Befehle

If the names are too long and they do not fit into the plot’s window you can increase it by using the option par:

boxplot(data, las = 2, par(mar = c(12, 5, 4, 2)+ 0.1),names = c(„Station 1″,“Station 2″,“Station 3″,“Station 4″,“Station 1″,“Station 2″,“Station 3″,“Station 4″,“Station 1″,“Station 2″,“Station 3″,“Station 4“))

Now I want to group the 4 stations so that the division in 3 successive days is clearer. To do that I can use the option at, which let me specify the position, along the X axis, of each box-plot:

boxplot(data, las = 2, at =c(1,2,3,4, 6,7,8,9,11,12,13,14), par(mar = c(12, 5, 4, 2) + 0.1), names =c(„Station 1″,“Station 2″,“Station 3″,“Station 4″,“Station 1″,“Station 2″,“Station 3″,“Station 4″,“Station 1″,“Station 2″,“Station 3″,“Station 4“))

Here I am specifying that I want the first 4 box-plots at position x=1, x=2, x=3 and x=4, then I want to leave a space between the fourth and the fifth and place this last at x=6, and so on.

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

Here is an example:

boxplot(data, las = 2, col= c(„red“,“sienna“,“palevioletred1″,“royalblue2″,“red“,“sienna“,“palevioletred1″,“royalblue2″,“red“,“sienna“,“palevioletred1″,“royalblue2″), at = c(1,2,3,4, 6,7,8,9, 11,12,13,14), par(mar = c(12,5, 4, 2) + 0.1), names = c(„Station 1″,“Station 2″,“Station 3″,“Station 4″,“Station 1″,“Station 2″,“Station 3″,“Station 4″,“Station 1″,“Station 2″,“Station 3″,“Station 4“))

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.

Let’s try it:

boxplot(data, ylab =„Oxigen (%)“, xlab =„Time“, las = 2, col= c(„red“,“sienna“,“palevioletred1″,“royalblue2″,“red“,“sienna“,“palevioletred1″,“royalblue2″,“red“,“sienna“,“palevioletred1″,“royalblue2″),at = c(1,2,3,4,6,7,8,9,11,12,13,14), par(mar = c(12, 5, 4, 2) + 0.1), names =c(„Station 1″,“Station 2″,“Station 3″,“Station 4″,“Station 1″,“Station 2″,“Station 3″,“Station 4″,“Station 1″,“Station 2″,“Station 3″,“Station 4“))

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

As you can see from the image above, the label on the Y axis is place very well and we can keep it. On the other hand, the label on the X axis is drawn right below the stations names and it does not look good.

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

The function mtext() requires 3 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 = 7)

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

The option line takes an integer with the line number, starting from 0 (which is the line closer to the plot axis). In this case I put the label onto the 7th line from the X axis.

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

The following is just one example:

data<-data.frame(Stat11=rnorm(100,mean=3,sd=2), Stat21=rnorm(100,mean=4,sd=1), 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,mean=4.5,sd=2), Stat32=rnorm(100,mean=7,sd=0.5), Stat42=rnorm(100,mean=8,sd=3), Stat13=rnorm(100,mean=6,sd=0.5), Stat23=rnorm(100,mean=5,sd=3), Stat33=rnorm(100,mean=8,sd=0.2), Stat43=rnorm(100,mean=4,sd=4))

data<-data.frame(Stat11=rnorm(100,mean=3,sd=2), Stat21=rnorm(100,mean=4,sd=1), 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,mean=4.5,sd=2), Stat32=rnorm(100,mean=7,sd=0.5), Stat42=rnorm(100,mean=8,sd=3), Stat13=rnorm(100,mean=6,sd=0.5), Stat23=rnorm(100,mean=5,sd=3), Stat33=rnorm(100,mean=8,sd=0.2), Stat43=rnorm(100,mean=4,sd=4))

boxplot(data, las = 2, col = c(„red“,“sienna“,“palevioletred1″,“royalblue2″,“red“,“sienna“,“palevioletred1″,“royalblue2″,“red“,“sienna“,“palevioletred1″,“royalblue2″), at = c(1,2,3,4, 6,7,8,9, 11,12,13,14), par(mar = c(12, 5, 4, 2) + 0.1), names = c(„“,““,““,““,““,““,““,““,““,““,““,““), ylim=c(-6,18)) #Station labelsmtext(„Station1″, side=1, line=1, at=1, 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=1, col=“red“)mtext(„Station2″, side=1, line=1, at=7, las=2, font=2, col=“sienna“)mtext(„Station3″, side=1, line=1, at=8, las=2, font=3, col=“palevioletred1“)mtext(„Station4″, side=1, line=1, at=9, las=2, font=4, col=“royalblue2“)mtext(„Station1″, side=1, line=1, at=11, las=2, font=1, 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=1, at=14, las=2, font=4, col=“royalblue2“) #Axis labelsmtext(„Time“, side = 1, line = 6, cex = 2, font = 3)mtext(„Oxigen (%)“, side = 2, line = 3, cex = 2, font = 3) #In-plot labelstext(1,-4,“*“)text(6,-4,“*“)text(11,-4,“*“) text(2,9,“A“,cex=0.8,font=3)text(7,11,“A“,cex=0.8,font=3)text(12,15,“A“,cex=0.8,font=3)

Boxplot R

http://www.r-bloggers.com/box-plot-with-r-tutorial/


Weitere wichtige Inhalte für Ihr Projekt!

Folgen Sie den einzelnen, unten stehenden Links und Sie gelangen zum Thema und den entsprechenden Excel Dateien. Das Bild mit allen Links zu den Themen können Sie als pdf (Werkzeuge_20150722_4_als_pdf) downloaden.
Die Excel Vorlagen für die Lean Tools laden Sie in dieser Excel Datei (Toolbox.xlsx) runter.
Das Komplettpaket Excel Vorlagen Messsystemanalyse und Prozessfähigkeit können Sie hier anfordern.

Alle Excel Vorlagen zur Messsystemanalyse und Prozessfähigkeit

Organisieren – Define Phase

Messen – Measure Phase

Analysieren – Measure Phase

Verbessern – Improve Phase

Weiterbildung

Excel Funktionen