2016-08-30 1 views
0

ich versuche, zwei verschiedene Farben in einem Boxplot zu verwenden. Mein Ziel ist es, den Teil über dem Median in rot und den Teil unter dem Median in grün zu färben. Momentan transformiere ich die Daten und kombiniere zwei Boxplots in einem Plot. Ich denke, das ist keine elegante Lösung. Vielleicht könnte mir jemand helfen? Hier ist mein R-Code:R Boxplot: Verwenden Sie verschiedene Farben über und unter dem Median

x <- rnorm(100) 
x_l <- x 
x_l[x_l > median(x)] <- median(x) 
boxplot(x_l, whiskcol = "darkgreen", staplecol = "darkgreen", 
     boxcol = "darkgreen", col = "darkgreen", ylim = c(-3, 3), 
     outcol="darkgreen", lwd = 2, medcol="black") 
x_u <- x 
x_u[x_l < median(x)] <- median(x) 
boxplot(x_u, whiskcol = "red", staplecol = "red", boxcol = "red", col = "red", 
     ylim = c(-3, 3), outcol="red", lwd = 2, medcol="black", add = TRUE) 

desired output image

Vielen Dank für Ihre Hilfe, freundlich Grüßen, CAWI

Antwort

1

Wie Sie gefunden haben, Ihr Ansatz Plots zwei Mediane erstellt, eine für jede Hälfte der Daten. Stattdessen berechnet eine boxplot, ohne sie zu Plotten, und ändern Sie dann die entsprechenden Elemente des Objekts, bevor bxp() mit plotten:

set.seed(0) 
x <- rnorm(100) 
top <- bottom <- boxplot(x,plot=FALSE) 
top$stats[1:2] <- top$stats[3] 
top$out <- top$out[top$out >= top$stats[3]] 
bottom$stats[4:5] <- bottom$stats[3] 
bottom$out <- bottom$out[bottom$out <= bottom$stats[3]] 
bxp(top, whiskcol = "darkgreen", staplecol = "darkgreen", 
     boxcol = "darkgreen", col = "darkgreen", ylim = c(-3, 3), 
     outcol="darkgreen", lwd = 2, medcol="black") 
bxp(bottom, whiskcol = "red", staplecol = "red", boxcol = "red", col = "red", 
     ylim = c(-3, 3), outcol="red", lwd = 2, medcol="black", add = TRUE) 
+0

Vielen Dank für Ihre schnelle Antwort! Warum sind nur die Ränder der Boxen farbig? – cawi

+0

Hinzufügen von 'boxfill =" red "' liefert das gewünschte Ergebnis – cawi

Verwandte Themen