[R] Boxplot with linear (not categorical) x-axis
William Dunlap
wdun|@p @end|ng |rom t|bco@com
Fri Sep 28 17:33:47 CEST 2018
Use the 'at' argument to boxplot. E.g.,
> x <- rep(c(2,4,8,16), c(5,10,20,30))
> y <- seq_along(x)
> par(mfrow=c(2,1))
> boxplot(y~x, at=unique(x))
> boxplot(y~x)
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Sep 28, 2018 at 3: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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list