[R] ggplot - boxplot and points split by two factors

Federico Lasa felasa at gmail.com
Mon Sep 8 20:57:03 CEST 2014


Use geom_jitter() instead of geom_point

On Mon, Sep 8, 2014 at 12:37 PM, Tom Wright <tom at maladmin.com> wrote:
> ggplot(data,aes(x = z1, y = x, fill=x2)) +
>   geom_boxplot() +
>   geom_point(alpha=0.5,
>         position=position_jitterdodge(jitter.width=0.1),
>         aes(group=x2))
>
> On Mon, 2014-09-08 at 13:10 -0400, Tom Wright wrote:
>> Hi,
>> I'm trying to create a boxplot overlayed with points where the data is
>> split by two factor groups.
>> So far:
>>
>> x1<-factor(rep(LETTERS[1:4],5))
>> x2<-factor(rep(letters[1:2],10))
>> z<-runif(20,0,10)
>>
>> data<-data.frame(x1=x1,x2=x2,z=z)
>>
>> ggplot(data,aes(x=x1,y=z,fill=x2)) +
>>       geom_boxplot() +
>>       geom_point()
>>
>>
>> Obviously I'd also like to separate the points to overlay the relevant boxplots.
>>
>> Any hints gratefully received.
>> Thanks,
>> Tom
>>
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list