I have a nearly-boxplot like jitter-plot:
dt <- rbind(se,cb,cb.se)
qplot(ds, size, data=dt, geom="jitter", colour=root, facets = test ~ .)
I'd love to put a summary label for each group in the middle of the plot - for example the size totals here:
aggregate(list(size=dt$size), list(dt$ds, dt$test), sum)
Group.1 Group.2 size
1 b217 se 9847
2 c10 se 97296
3 c613 se 21633
4 c7 se 207540
...
I've tried using + geom_text(aes(x=ds, y=128, label=sum(size)), size=2)
to add labels, but I get the same label on each position - how can I get the sum of just that section of data?
Edit: Here's where I'm at now - maybe I'm just going in the wrong direction
data <- rbind(se,cb,cb.se)
labels <-ddply(data, c("ds", "test"), function(df) sum(df$size))
ggplot(data=data, aes(x=ds)) +
geom_jitter(aes(y=size, colour=root)) +
geom_text(data=labels, aes(x=ds, y=600, label=V1), size=3) +
facet_wrap(test ~ .)
This code doesn't work - I get an undefined columns selected
error... somewhere. Maybe it's because of the multiple data=
sections?
Since you don't provide sample data, I shall demonstrate a solution using random data.
set.seed(1)
n <- 100
dat <- data.frame(
ds = sample(paste("x", 1:8, sep=""), n, replace=TRUE),
size = runif(n, 0, 250),
root = sample(c(TRUE, FALSE), n, replace=TRUE),
test = sample(c("se", "cb", "cb.se"), n, replace=TRUE)
)
head(dat)
ds size root test
1 x3 163.68098 TRUE cb.se
2 x3 88.29932 TRUE se
3 x5 67.56504 FALSE cb
4 x8 248.17102 TRUE cb
5 x2 158.37332 TRUE cb
6 x8 53.30203 FALSE cb.se
p <- ggplot(dat, aes(x=ds, y=size)) +
geom_jitter(aes(colour=root)) +
facet_grid(test~.)
Create the data frame containing label data. Note the use of summarize
. This tells ddply
to create a new column to the data.frame
labels <- ddply(dat, .(ds, test), summarize, size=round(sum(size), 0))
head(labels)
ds test size
1 x1 cb 193
2 x1 cb.se 615
3 x1 se 274
4 x2 cb 272
5 x2 cb.se 341
6 x2 se 1012
p + geom_text(aes(x=ds, label=size, y=128), data=labels, size=2)
Take a look here. It may be helpful Adding direct labels to ggplot2 and lattice plots
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With