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

Richard M. Heiberger rmh @end|ng |rom temp|e@edu
Fri Sep 28 15:45:36 CEST 2018


install.packages("HH")
library(HH)
system.file("demo/bwplot.examples.r", package="HH")
demo("bwplot.examples", package="HH", ask=FALSE)

## your example
dfA <- data.frame(X, Y=c(A, B, C))
dfA$X.factor <- factor(dfA$X)
position(dfA$X.factor) <- c(1,3,5)
bwplot(Y ~ X.factor, panel=panel.bwplot.intermediate.hh, data=dfA, xlim=c(0,6))

On Fri, Sep 28, 2018 at 6:05 AM, Luigi Marongiu
<marongiu.luigi using gmail.com> wrote:
> 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
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list