[R] Boxplot using Formula
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Sun Apr 9 11:43:28 CEST 2006
Matt Goff wrote:
> I am trying to use the formula interface for the boxplot.
> Currently running R 2.2.1 on Windows XP.
>
> The problem is that boxplot is displaying groups that are empty in the
> plot.
>
> The following example demonstrates what it is happening (though my actual
> situation is a little more complicated):
>
>
>>data<-data.frame(values=c(1:25), groups=rep(c("A","B","C","D","E"),
>>each=5))
>>boxplot(data$values~data$groups, subset=data$groups!="C")
>
>
> The plot includes a space for level "C" with no boxplot. I would like to
> have only those levels with associated boxplots be shown in the figure.
> So in this case, there should only be values "A", "B", "D", and "E" evenly
> spaced along the horizontal axis.
>
> I am a little confused because I am reusing old code and I don't remember
> having this issue before, so perhaps there have been some changes in the
> behavior of the boxplot function since the last time I use the code.
>
> It seems like there's probably a simple solution, but I'm not seeing it.
>
> Thanks,
>
> Matt Goff
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Hi, Matt,
I'm afraid you cannot do what you want with boxplot.formula. You have at
least two options:
1) Supply only data that has no empty levels.
2) Create a new function that does what you need:
## add `drop.unused.levels' argument to boxplot.formula
boxplot2 <-
function (formula, data = NULL, ...,
subset, na.action = NULL,
drop.unused.levels = TRUE)
{
if (missing(formula) || (length(formula) != 3))
stop("'formula' missing or incorrect")
m <- match.call(expand.dots = FALSE)
if (is.matrix(eval(m$data, parent.frame())))
m$data <- as.data.frame(data)
m$... <- NULL
m$na.action <- na.action
m$drop.unused.levels <- drop.unused.levels
m[[1]] <- as.name("model.frame")
mf <- eval(m, parent.frame())
response <- attr(attr(mf, "terms"), "response")
boxplot(split(mf[[response]], mf[-response]), ...)
}
values <- 1:25
groups <- factor(rep(c("A","B","C","D","E"), each=5))
boxplot2(values ~ groups, subset = groups != "C")
HTH,
--sundar
More information about the R-help
mailing list