Leo
Leo

Reputation: 121

How to manually set the scale for multiple boxplots in lattice bwplot?

I've got the results of clustering and decided to make a boxplot for each cluster, using lattice. Next, I was faced with the need to establish a scale, acceptable to all cluster boxplots.

First attempt

Found a solution, which allows to exclude an outliers and set free relation.

library(lattice)
trellis.device(new=FALSE, col=FALSE)

bwplot(value ~ variable | Cluster, data = test, 
       layout = c(2,2), 
       prepanel = function(x, y) {
               bp <- boxplot(split(y, x), plot = FALSE)
               ylim <- range(bp$stats)
               list(ylim = ylim) },
       scales = list(y = list(relation = "free")),
       do.out = F)

So, I've got pretty good plots, but it can be better, if I manually set the ylim for each plot. Eg there is only integer values in my data and the value 0.5 at upper left cluster graph is meaningless. Second

So, is there any way to set multiple ylims in bwplot parameters?

Upvotes: 2

Views: 5080

Answers (1)

BenBarnes
BenBarnes

Reputation: 19454

From the documentation under ?bwplot:

xlim could also be a list, with as many components as the number of panels (recycled if necessary), with each component as described above. This is meaningful only when scales$x$relation is "free", in which case these are treated as if they were the corresponding limit components returned by prepanel calculations.

The ylim argument has the corresponding functionality for the y-axis.

So, set relation = "free" in the scales argument as you did, and then pass a list to the ylim argument to individually set the y-axis limits for each panel:

bwplot(len ~ factor(dose) | supp, data = ToothGrowth,
    scales = list(relation = "free"),
    ylim = list(c(5, 31), c(0, 36)))

enter image description here

Upvotes: 7

Related Questions