Re: [R] ggplot geom_boxplot and stat_smooth

2011-01-25 Thread Petr PIKAL
Thank you

I did not realise I can simply add data from different source data frame 
to constructed graph. It even works with stat_smooth() quite 
straightforward. One is always learning new tricks.

Best regards
Petr

r-help-boun...@r-project.org napsal dne 25.01.2011 10:24:42:

> Hi Petr:
> 
> I had to do a little bit of finagling, but this seems to work. I 
basically
> did the following:
> 
> (i) coordinated the dodging of the points and boxplots by typ within 
konc.f;
> (ii) summarized the group medians in a separate data frame and added an
> additional
> column to compensate for the offsets in the boxplots due to dodging
> (iii) used geom_line() with group typ to get the connecting lines.
> 
> # library(ggplot2)
> 
> # Summarize group medians
> ads <- ddply(ad, .(konc.f, typ), summarise, m = median(bel))
> # Offset x-positions
> ads$pos <- rep(c(1:4, 6, 7), each = 4) + rep(c(-0.3, -0.1, 0.1, 0.3), 6)
> 
> # Note use of position_dodge() with same width in both the boxplot and 
point
> geoms
> # Add geom_line with summarized data frame and offset x-positions
> # group = typ produces a separate line per typ
> 
> p + geom_boxplot(position = position_dodge(width = 0.8)) +
> geom_point(position = position_dodge(width = 0.8)) +
> geom_line(data = ads, aes(x = pos, y = m, colour = typ, group = 
typ))
> 
> I chose to create a separate data frame for the summaries rather than 
use
> stat_summary(), for example, because it is a bit easier to plot 
individual
> layers that way.
> 
> HTH,
> Dennis
> 
> On Mon, Jan 24, 2011 at 11:05 PM, Petr PIKAL  
wrote:
> 
> > Dear all
> >
> > I would like to superpose some smoothing line through boxplot in 
ggplot
> >
> > > dput(ad)
> > structure(list(konc.f = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
> > 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L,
> > 7L, 7L, 7L, 7L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
> > 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L,
> > 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L,
> > 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
> > 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 7L, 7L, 7L), .Label =
> > c("(189,196]",
> > "(196,202]", "(202,208]", "(208,215]", "(215,221]", "(221,227]",
> > "(227,234]"), class = "factor"), bel = c(99.28, 99.29, 99.25,
> > 98.13, 99.51, 99.21, 99.09, 97.84, 98.97, 98.48, 98.64, 98.09,
> > 98.44, 98.19, 98.25, 97.54, 99.11, 98.23, 97.62, 97.01, 97.62,
> > 97.58, 97.42, 97.75, 97.16, 96.79, 96.82, 98.8, 99.02, 98.86,
> > 98.85, 99.25, 98.46, 98.68, 98.3, 98.67, 98.54, 98.39, 98.18,
> > 98.54, 99.13, 98.92, 98.29, 98.78, 98.58, 98.78, 98.9, 98.18,
> > 97.5, 98.63, 97.53, 96.55, 96.52, 96.23, 95.54, 95.54, 96.33,
> > 95.91, 95.41, 94.98, 94.69, 93.95, 94.05, 95.22, 94.6, 94.27,
> > 93.44, 95.15, 94.62, 93.86, 94.51, 94.83, 93.66, 92.95, 94.4,
> > 93.17, 92.77, 95.79, 95.03, 94.96, 95.94, 95.71, 95.19, 95.11,
> > 94.91, 94.42, 94.68, 94.91, 94.66, 94.05, 93.57, 93.43, 94.77,
> > 93.58, 93.84, 93.24, 94.45, 93.57, 93.46, 92.38, 92.39, 94.07
> > ), typ = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> > 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> > 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> > 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L,
> > 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> > 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> > 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("alrut",
> > "nealrut", "stan", "vlakan"), class = "factor")), .Names = c("konc.f",
> > "bel", "typ"), row.names = c(NA, -102L), class = "data.frame")
> > >
> >
> > Here is what I did
> >
> > p<-ggplot(ad, aes(x=konc.f, y=bel, colour=typ))
> > 
p+geom_boxplot()+geom_jitter(position=position_jitter(w=0.1))+stat_smooth()
> > geom_smooth: Only one unique x value each group.Maybe you want 
aes(group =
> > 1)?
> >
> > I get nice picture with boxes but I expected to get something like
> > smoothing line through box centres.
> >
> > Is it possible without some hack to stat_smooth code?
> >
> > Regards
> > Petr
> >
> > __
> > R-help@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.
> >
> 
>[[alternative HTML version deleted]]
> 
> __
> R-help@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.

__
R-help@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 comm

Re: [R] ggplot geom_boxplot and stat_smooth

2011-01-25 Thread Dennis Murphy
Hi Petr:

I had to do a little bit of finagling, but this seems to work. I basically
did the following:

(i) coordinated the dodging of the points and boxplots by typ within konc.f;
(ii) summarized the group medians in a separate data frame and added an
additional
column to compensate for the offsets in the boxplots due to dodging
(iii) used geom_line() with group typ to get the connecting lines.

# library(ggplot2)

# Summarize group medians
ads <- ddply(ad, .(konc.f, typ), summarise, m = median(bel))
# Offset x-positions
ads$pos <- rep(c(1:4, 6, 7), each = 4) + rep(c(-0.3, -0.1, 0.1, 0.3), 6)

# Note use of position_dodge() with same width in both the boxplot and point
geoms
# Add geom_line with summarized data frame and offset x-positions
# group = typ produces a separate line per typ

p + geom_boxplot(position = position_dodge(width = 0.8)) +
geom_point(position = position_dodge(width = 0.8)) +
geom_line(data = ads, aes(x = pos, y = m, colour = typ, group = typ))

I chose to create a separate data frame for the summaries rather than use
stat_summary(), for example, because it is a bit easier to plot individual
layers that way.

HTH,
Dennis

On Mon, Jan 24, 2011 at 11:05 PM, Petr PIKAL  wrote:

> Dear all
>
> I would like to superpose some smoothing line through boxplot in ggplot
>
> > dput(ad)
> structure(list(konc.f = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
> 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L,
> 7L, 7L, 7L, 7L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L,
> 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L,
> 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
> 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 7L, 7L, 7L), .Label =
> c("(189,196]",
> "(196,202]", "(202,208]", "(208,215]", "(215,221]", "(221,227]",
> "(227,234]"), class = "factor"), bel = c(99.28, 99.29, 99.25,
> 98.13, 99.51, 99.21, 99.09, 97.84, 98.97, 98.48, 98.64, 98.09,
> 98.44, 98.19, 98.25, 97.54, 99.11, 98.23, 97.62, 97.01, 97.62,
> 97.58, 97.42, 97.75, 97.16, 96.79, 96.82, 98.8, 99.02, 98.86,
> 98.85, 99.25, 98.46, 98.68, 98.3, 98.67, 98.54, 98.39, 98.18,
> 98.54, 99.13, 98.92, 98.29, 98.78, 98.58, 98.78, 98.9, 98.18,
> 97.5, 98.63, 97.53, 96.55, 96.52, 96.23, 95.54, 95.54, 96.33,
> 95.91, 95.41, 94.98, 94.69, 93.95, 94.05, 95.22, 94.6, 94.27,
> 93.44, 95.15, 94.62, 93.86, 94.51, 94.83, 93.66, 92.95, 94.4,
> 93.17, 92.77, 95.79, 95.03, 94.96, 95.94, 95.71, 95.19, 95.11,
> 94.91, 94.42, 94.68, 94.91, 94.66, 94.05, 93.57, 93.43, 94.77,
> 93.58, 93.84, 93.24, 94.45, 93.57, 93.46, 92.38, 92.39, 94.07
> ), typ = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("alrut",
> "nealrut", "stan", "vlakan"), class = "factor")), .Names = c("konc.f",
> "bel", "typ"), row.names = c(NA, -102L), class = "data.frame")
> >
>
> Here is what I did
>
> p<-ggplot(ad, aes(x=konc.f, y=bel, colour=typ))
> p+geom_boxplot()+geom_jitter(position=position_jitter(w=0.1))+stat_smooth()
> geom_smooth: Only one unique x value each group.Maybe you want aes(group =
> 1)?
>
> I get nice picture with boxes but I expected to get something like
> smoothing line through box centres.
>
> Is it possible without some hack to stat_smooth code?
>
> Regards
> Petr
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.