ggplot(data, aes(x=z, y=x, colour=yy)) + geom_boxplot(position="dodge")
Unfortunately, position="dodge" doesn't do anything useful in
geom_point, I'd love to know if there's a way to apply the effect that
it has on geom_boxplot to geom_point.
zw
> --
> 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
>
I would probably use faceting:
##
library(ggplot2)
se <-function(x) sqrt(var(x)/length(x))
x <-runif(270, 0, 125)
dat <- as.data.frame(x)
dat$z <- factor(c("A","A","A","B","B","B","C","C","C"))
dat$yy <- factor(c("a","b","c"))
ggplot(data=dat, aes(x=yy, y=x)) + geom_boxplot() +
geom_jitter(position=position_jitter(width=0.1)) +
facet_wrap(~z,nrow=1)+theme_bw()
##
This gets around the lack of dodging that Zack noted.
Jonathan
If any one was interested, I could dig up the references that discuss
how to create the dodged points in that plot. I haven't had time to
implement the algorithm, but it isn't a huge job.
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
I agree with Jonathan that faceting is probably the easiest way around
the dodge issue, also it makes it very clear to read, I think. Here
is another option with jittered points, and a bigger point for the
mean and a line for +/- se. Rather than type that all out, I just
edited your se function and then passed it to stat_summary()
Cheers,
Josh
library(ggplot2)
dat <- data.frame(x = runif(270, 0, 125), z = rep(LETTERS[1:3], each = 3),
yy = letters[1:3], stringsAsFactors = TRUE)
## define summary function for mean +/- SE
smry <-function(x) {
mu <- mean(x)
se <- sqrt(var(x)/length(x))
return(data.frame(y = mu, ymin = mu - se, ymax = mu + se))
}
## small data points, big mean point + line
ggplot(data = dat, aes(x = yy, y = x)) +
geom_jitter(position = position_jitter(width = 0.1), size = 1) +
stat_summary(fun.data = smry, size = .9) +
facet_wrap(~ z, nrow = 1) +
theme_bw()
> --
> 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
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
For the record, here (below) are the ggplot versions of the (base
plotting) examples I have up at
<http://emdbolker.wikidot.com/blog:dynamite>.
On my wish list for new geoms etc.:
* the 'point dodging' stuff from
<http://biostat.mc.vanderbilt.edu/wiki/Main/TatsukiRcode>
* a dedicated 'geom_pie' (as opposed to the coord_polar() transform),
for superimposing pie charts on maps [I do believe this is actually
sometimes a good idea -- little mini barplots might be marginally
better, but population geneticists are used to the pies]
* a dedicated geom_violin rather than the geom_ribbon hack below
* a geom_beanplot?
is there thought to be any point to boxplots-with-notches any more, or
is that too 1980s?
I guess I should start studying the ggExtra package to figure out how
to write these myself ...
===========
library(ggplot2)
library(MASS)
theme_set(theme_bw())
g0 <- ggplot(OrchardSprays,aes(x=treatment,y=decrease))+
scale_y_log10()
g_dyn <- g0 +
stat_summary(fun.data=mean_cl_normal,geom="bar",colour="gray")+
stat_summary(fun.data=mean_cl_normal,geom="errorbar",width=0.5)
g_errbar <- g0 + stat_summary(fun.data=mean_cl_normal,geom="pointrange")
mm1 <- function(...) {
mean_cl_normal(...,mult=1)
}
mm2 <- function(...) {
mean_cl_normal(...,mult=2)
}
g_errbar2 <- g0 +
stat_summary(fun.data=mm1,geom="linerange",lwd=1)+
stat_summary(fun.data=mm2,geom="linerange")+
stat_summary(fun.data=mm1,geom="point")
g_point <- g0 +geom_point()
g_boxplot <- g0 + geom_boxplot()
g_violin <- ggplot(OrchardSprays,
aes(x=log10(decrease)))+
geom_ribbon(aes(ymax = ..density.., ymin = -..density..),
stat = "density")+
facet_grid(. ~ treatment, as.table = FALSE,
scales = "free_y")+
opts(panel.margin=unit(0 , "lines"))+
coord_flip()+opts(axis.text.x=theme_blank())
library(gridExtra)
grid.arrange(g_dyn,g_errbar,g_errbar2,g_point,g_boxplot,g_violin,
nrow=2)
I've been looking for a way to contribute, and it looks like several
people are interested, so I'll bite. Any other references/tips you
think would help in general with understanding how to work with ggplot
would also be appreciated.
Thanks,
Josh
>
> Hadley
If you wanted to write your own, the best place to start would be the
new layers package - https://github.com/hadley/layers. This will
provide the implementation of geom, stats and position adjustments
from ggplot2 0.9, and has been heavily rewritten to be much simpler
and uses much better development practices (e.g. S3 instead of proto,
uses roxygen, namespaces etc). Start with
https://github.com/hadley/layers/blob/master/R/geom.r for
documentation about the methods.
Instead of geom_pie, I'd suggest geom_wedge, and look at the
parameters protovis uses
(http://vis.stanford.edu/protovis/docs/wedge.html). You could write a
new position adjustment for the special case of pies that rotates each
wedge so they don't overlap.
For density estimation, I'd check out https://github.com/hadley/bin
(probably soon to be renamed), where I've been trying provide a common
interface to density estimation across all R packages that do it.
This is even more under development, but hopefully you can see the
general idea.
Awesome! I'd start with these two papers from Lee Wilkinson:
@article{wilkinson:1999a,
Author = {Wilkinson, Leland},
Journal = {The American Statistician},
Title = {Dot plots},
Year = {1999}}
@article{dang:2010,
Author = {Dang, Tuan Nhon and Wilkinson, L and Anand, A.},
Journal = {IEEE Transactions on Visualization and Computer Graphics},
Number = {6},
Pages = {1044-1052},
Title = {Stacking Graphic Elements to Avoid Over-plotting},
Volume = {16},
Year = {2010}}
I would start by getting the basic algorithm working and testing it
with base graphics. Integration with ggplot2 should happen once
everything else is working - but if you are interested, I'd again
recommend looking at https://github.com/hadley/layers - position is
the least development of the three, but again it should give you some
idea where I'm going.
It looks like an interesting approach, but it's not usable with
ggplot2 because it uses base graphics. It's also frustratingly
written - the data transformation is all tangled up with the plotting
which makes it very difficult to see what's going on. Do you know if
this method has been published anywhere?
d2$x2 <- d2$x
dz <- ggplot(d2, aes(x = junk, colour = z))
dz + geom_density(fill = NA) + facet_grid(x2 ~ .)
--
Kohske Takahashi <takahash...@gmail.com>
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
The problem is that facetting columns are added on to the data frame
containing the aesthetics - this needs a complete redesign to fix (in
progress in the main branch), where instead of adding on all the
facetting variables, we just add on a column that gives the panel that
the data belongs to.
-Aaron