[R] plot multiple data sets on same axis

Scott Lamb slamb at slamb.org
Mon Dec 31 05:32:40 CET 2007

Scott Lamb wrote:
> Scott Lamb wrote:
>> I've tried replacing the for loop body with this:
>>     this_method <- split.df[[i]]
>>     boxplot(elapsed~inactive, data=this_method,
>>             add=TRUE, border=i, boxfill=i, outline=FALSE)
>> but it has two problems:
>> * it doesn't plot at the correct x values. It looks like I need to
>> supply a list of the x values as the "at" parameter, and I don't know
>> how to get unique values from this_method$inactive. (I tried the clunky
>> labels(split(this_method, this_method$inactive)), but it returns them as
>> strings.)
> Ahh. I missed the obvious answer - there's a function called unique.
> at=unique(this_method$inactive) works.
>> * it redraws the graph's frame, and it ignores bty="l" when doing so.
> also the x axis tics and labels...they're totally unreadable now.

Ahh. There is an "axis=FALSE" parameter to bxp, which boxplot passes
along. Thanks again, and sorry for all the list noise.

For the record, here's exactly what I did to duplicate the original graph:

df <- read.csv("http://www.slamb.org/tmp/one-active.csv")
png(filename="one-active.png", width=800, height=600)
split.df <- split(df, df$method)
plot(0, xlim=c(0, max(df$inactive)), ylim=range(df$elapsed),
     ylab="time (µs)", xlab="inactive file descriptors", log="y",
     main="1 active descriptor, 1 write", bty="n", type="n")
for (i in seq_along(split.df)) {
    this_method <- split.df[[i]]
    unique_inactive <- unique(this_method$inactive)
    boxplot(elapsed~inactive, data=this_method,
            at=unique_inactive, axes=FALSE,
            add=TRUE, border=i, boxfill=i, outline=FALSE, bty="l",
            whisklty="solid", staplelty="blank", medlty="blank",
legend("topleft", legend=labels(split.df),
       fill=seq_along(split.df), bty="n")


Scott Lamb <http://www.slamb.org/>

More information about the R-help mailing list