[R] Boxplot with linear (not categorical) x-axis

Luigi Marongiu m@rongiu@luigi @ending from gm@il@com
Fri Sep 28 12:05:48 CEST 2018


Dear all,
I am using boxplot to draw some data. Would be possible to have the
X-axis linear (as in a scatter plot) instead of the standard
categorical axis?
The data I have is subdivided into three groups at the numerical
values 1, 3, 5; boxplot treats these as categorical values; in fact, I
can write my own labels simply by using the values 1, 2, 3 for the
position of the labels as in the example.
Thank you,

>>>>
# generate data.frames
A = c(70, 22, 4, 21, 29, 35, 24, 20, 9, 21,
      22, 12, 20, 21, 13, 18, 15, 3, 9, 23,
      6, 5, 2, 24, 25, 21, 16, 0, 4, 1)
B = c(17, 21, 70, 6, 23, 10, 8, 5, 22, 5,
      21, 5, 19, 9, 23, 24, 11, 13, 7, 15,
      25, 9, 13, 14, 11, 9, 12, 0, 5, 9)
C = c(17, 8, 30, 22, 11, 32, 33, 8, 160, 11,
      35, 7, 36, 15, 11, 25, 16, 6, 38, 19,
      35, 30, 12, 27, 22, 32, 47, 39, 31, 26)
X = rep(c(1, 3, 5),30*3)
dfA <- data.frame(X, c(A, B, C))
names(dfA) <- c("X", "Y")
par(mfrow=c(2,1))
boxplot(dfA$Y ~ dfA$X,
        ylim=c(0, 80),
        col="green",
        ylab="Y-values",
        xlab="X-values",
        main="usual X labels"
)
boxplot(dfA$Y ~ dfA$X,
        ylim=c(0, 80),
        col="green",
        ylab="Y-values",
        xlab="X-values",
        main="custom X labels",
        xaxt="n"
)
x.lab = c("A", "B", "C")
x.pos = c(1, 2, 3)
axis(side=1, at=x.pos,
     lab=x.lab, cex.axis=1)

-- 
Best regards,
Luigi



More information about the R-help mailing list