I have a list of matrices I wish to plot. Each element in the list ultimately represents a facet to be plotted. Each matrix element has dimensions Row * Col, where all values in a row are grouped and plotted as a scatterplot (i.e. X-axis is categorical for the row names, Y-axis is the value, Col. values per row).
开发者_Go百科Additionally, I would like to add the CV for the distribution of points at each X.
X1 X2 value L1
a subject1 8.026494 facet1
b subject1 7.845277 facet1
c subject1 8.189731 facet1
(10 categorical groupings - a-j)
a subject2 5.148875 facet1
b subject2 8.023356 facet1
(33 subjects plotted for each categorical grouping)
a subject1 5.148875 facet2
b subject1 8.023356 facet2
(multiple facets (in my specific case, 50) with identical categorical grouping and subject names)
I managed to plot this to my satisfaction with the following:
p <- (qplot(X1, value, data=melt(df), colour=X2)
+ facet_wrap(~Probeset, ncol=10, nrow=5, scales="free_x"))
However, I would like to add the CV of each grouping of points along the X-axis as a label hovering above the group. I tried variations on this:
p + geom_text(aes(x=X1, y="arbitrary value at the top of the Y-axis scale", label="vector of labels")))
But none of them behaved as I wish. How would I go about getting the CV of each group above the group of points, as a label?
Thank you in advance!
Since there is no X2
corresponding to each label, you have to put the labels into a separate data set, and supply it in the data
argument of geom_text
. Using a reproducible example:
library(ggplot2)
#create data with the desired structure
dd <- expand.grid(facet=LETTERS[1:4], group=letters[1:5], subject=factor(1:10))
dd$value <- exp(rnorm(nrow(dd)))
#calculate CV's
ddcv <- ddply(dd, .(facet,group),
function(x)c(CV=sd(x$value)/mean(x$value), maxX=max(x$value)))
ddcv$CV <- round(ddcv$CV,1)
#make plots
p <- qplot(group, value, colour=subject, data=dd) + facet_wrap(~facet)
p + geom_text(aes(x=group, y=maxX+1, label=CV), colour="black", data=ddcv)
精彩评论