Passing values to geom_text & getting them back properly

10 views
Skip to first unread message

Bryan Hanson

unread,
Jul 28, 2010, 9:15:06 PM7/28/10
to ggplot2
Hello All.. I need some assistance with passing values to geom_text. Test
data and function below. I want the counts displayed at the bottom of the
plot to reflect both the levels of fac1 and fac2, but the current code gives
the total at a given level of fac2, not split out by fac1.

I've tried many variations on this, the one appended below is pretty lame
and mixes an aesthetic approach with a more hard-wired approach, but it
comes close to illustrating the effect I want, even if the code is lousy.

Thanks for any suggestions! Bryan
*************
Bryan Hanson
Acting Chair
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA

# test data
res = c(rnorm(10, 5, 1.5), rnorm(10, 8, 2), rnorm(10, 14, 2.0), rnorm(10,
10, 1.5), rnorm(10, 15, 2), rnorm(10, 12,2.5))
fac1 <- c(rep("L", 20), rep("M", 20), rep("H", 20))
fac1 <- factor(fac1, levels = c("L", "M", "H"))
fac2 <- c(rep("WT", 10), rep("GM", 10))
fac2 <- as.factor(rep(fac2, 3))
td <- data.frame(r = res, f1 = fac1, f2 = fac2)
td[c(13,32:33, 55:58), 1] <- NA
td <- na.omit(td)
count(td, vars = c("f1", "f2")) # shows values desired on plot

#########
rxnNorm <- function(data = NULL, res = NULL,
fac1 = NULL, fac2 = NULL, fac2cols = NULL, ...) {

p <- ggplot(data, aes_string(x = fac1, y = res, color = fac2, group =
fac2))
p <- p + stat_summary(fun.y = "mean", geom = "line") + geom_point()
p <- p + scale_colour_manual(name = "", values = fac2cols)

# now add the counts for each level of fac1 x fac2

gl <- levels(data[,fac2])
ng <- length(gl)
pos <- 1:ng*0.03
a <- min(data[,res])
b <- diff(range(data[,res]))

for (i in 1:ng) {
p <- p + geom_text(aes_string(x = fac1,
y = a - pos[i] * b, group = (data[,fac2] == gl[i]),
label = 'paste("n = ", ..count.. , sep = "")'), stat = "bin",
size = 4.0, data = data, color = fac2cols[i])
}

invisible(p)
}

########
tst <- rxnNorm(data = td, res = "r", fac1 = "f1", fac2 = "f2", fac2cols =
c("red", "blue")); print(tst)


takahashi kohske

unread,
Jul 28, 2010, 9:42:26 PM7/28/10
to Bryan Hanson, ggplot2
hi,

please try this:

ggplot(td,aes(f1,r,colour=f2,group=f2))+stat_summary(fun.y="mean",geom="line")+geom_point()+stat_bin(aes(x=f1,y=0,label=..count..),geom="text",position=position_dodge(width=1),legend=F)

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

takahashi kohske

unread,
Jul 28, 2010, 9:48:46 PM7/28/10
to Bryan Hanson, ggplot2
and maybe vertical positioning of the texts you hope is like this:

ggplot(td,aes(f1,r,colour=f2,group=f2))+stat_summary(fun.y="mean",geom="line")+geom_point()+stat_bin(aes(x=f1,y=c(-0.5,0.5)[as.numeric(f2)],label=paste("n
=",..count..)),geom="text",legend=F)

Reply all
Reply to author
Forward
0 new messages