[R] boxplot with "cut"

Rui Barradas ruipbarradas at sapo.pt
Tue Jul 10 11:24:12 CEST 2012


Hello,

Maybe this iss what you're looking for. GD is your data.frame.



multi.boxplot <- function(x, by, ...){
	x <- as.data.frame(x)
	sp <- split(x, by)
	len <- length(sp) - 1
	n <- ncol(x)
	n1 <- n + 1
	boxplot(x[[ 1 ]] ~ by, at = 0:len*n1 + 1,
		xlim = c(0, (len + 1)*n1), ylim = range(unlist(x)), xaxt = "n", ...)
	for(i in seq_len(n)[-1])
		boxplot(x[[i]] ~ by, at = 0:len*n1 + i, xaxt = "n", add = TRUE, ...)
	axis(1, at = 0:len*n1 + n1/2, labels = names(sp), tick = TRUE)
}

cols <- grep("ReadCount", names(GD))
multi.boxplot(GD[, cols], cut(GD$GeneDensity, breaks=10))


If this is it and you don't like those x-axis tick lables, use 
as.integer(cut(...etc...)).

Hope this helps,

Rui Barradas

Em 09-07-2012 20:51, Vining, Kelly escreveu:
> Dear UseRs,
> I'm making box plots from a data set that looks like this:
>
>
>    Chr Start   End GeneDensity ReadCount_Explant ReadCount_Callus ReadCount_Regen
> 1   1     1 10000      107.82             1.243            1.047           1.496
> 2   1 10001 20000      202.50             0.835            0.869           0.456
> 3   1 20001 30000      158.80             1.813            1.529           1.131
> 4   1 30001 40000      100.53             1.731            1.752           1.610
> 5   1 40001 50000      100.53             3.056            2.931           3.631
> 6   1 50001 60000      100.53             1.960            2.013           2.459
>
> I'm breaking the "GeneDensity" column into deciles, then making a box plot of the relationship between the GeneDensity parameter and each of the three "ReadCount" columns. Here's an example of one of my boxplot commands:
>
> boxplot(GeneDensity$ReadCount_Explant ~ cut(GeneDensitySorted$GeneDensity, breaks=10), ylim=c(0,40), ylab="RPKM", xlab="GENE DENSITY (LOW -> HIGH)", main="INTERNODE EXPLANT")
>
> Right now, I'm making three separate graphs: one for each of the three "ReadCount" columns. I'd like to put all three sets on one graph, so that each decile is represented by three boxes, one for each ReadCount category, but don't know how to make that work. I tried this:
>
> boxplot(GeneDensitySorted$ReadCount_Explant ~ cut(GeneDensitySorted$GeneDensity, breaks=10), GeneDensitySorted$ReadCount_Callus ~ cut(GeneDensitySorted$GeneDensity, breaks=10), GeneDensitySorted$ReadCount_Regen ~ cut(GeneDensitySorted$GeneDensity, breaks=10), ylim=c(0,40), ylab="RPKM", xlab="GENE DENSITY (LOW -> HIGH)")
>
> Not surprisingly, I got this error:
>
> Error in as.data.frame.default(data) :
>    cannot coerce class '"formula"' into a data.frame
>
> Does anyone know how to accomplish this box plot?
>
> Any help is much appreciated.
>
> --Kelly V.
> ______________________________________________
> R-help at r-project.org mailing list
> 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