skifond
unread,Mar 26, 2012, 5:48:04 PM3/26/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ggplot2
Hi all!
This is my first post on ggplot2.
I am trying to add the mean to a violin/boxplot graphic. When I have
only one factor it is working fine but when I have two factors the
mean of each factor appears only in the middle (not in the box or
violin plots). I played with alpha and width with no luck. My second
problem is that the boxplots are not inside the violin plots.
Any help would be really appreciated.
martin
library(ggplot2)
library(scales)
library(plyr)
A reproductible example (data=test in the code below):
structure(list(Site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label =
c("Site1",
"Site2", "Site3"), class = "factor"), Treatment = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("Control", "test"), class = "factor"),
Value = c(4L, 3L, 11L, 22L, 6L, 7L, 3L, 9L, 6L, 10L, 20L,
30L, 40L, 2L, 4L, 5L, 8L, 10L, 21L, 30L, 2L, 5L, 8L, 10L,
3L, 4L, 5L, 6L, 7L, 8L, 22L, 21L, 20L, 14L, 5L, 3L)), .Names =
c("Site",
"Treatment", "Value"), class = "data.frame", row.names = c(NA,
-36L))
My code:
ggplot(test, aes(Site, Value)) + geom_violin(aes(fill=Treatment))
+ geom_boxplot (aes(fill=Treatment), alpha=1, width=0.5) + ylim(0,50)
+ opts(legend.position=c(0.1,0.7)) + opts(axis.title.x =
theme_blank())
+ stat_summary(aes(group=Treatment), fun.y=mean, geo="point",
fill="black", shape=21, size=3)