ggplot2 - jitter and position dodge together

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I am trying to recreate a figure from a GGplot2 seminar http://dl.dropbox.com/u/42707925/ggplot2/ggplot2slides.pdf.

In this case, I am trying to generate Example 5, with jittered data points subject to a dodge. When I run the code, the points are centered around the correct line, but have no jitter.

Here is the code directly from the presentation.

set.seed(12345) hillest

回答1:

EDIT: There is a better solution with ggplot2 version 1.0.0 using position_jitterdodge. See @Didzis Elferts' answer. Note that dodge.width controls the width of the dodging and jitter.width controls the width of the jittering.

I'm not sure how the code produced the graph in the pdf.

But does something like this get you close to what you're after?

I convert tailindex and memorypar to numeric; add them together; and the result is the x coordinate for the geom_jitter layer. There's probably a more effective way to do it. Also, I'd like to see how dodging geom_boxplot and geom_jitter, and with no jittering, will produce the graph in the pdf.

library(ggplot2) dodge 



回答2:

In ggplot2 version 1.0.0 there is new position named position_jitterdodge() that is made for such situation. This postion should be used inside the geom_point() and there should be fill= used inside the aes() to show by which variable to dodge your data. To control the width of dodging argument dodge.width= should be used.

ggplot(ex5,aes(x=tailindex ,y=hillest,color=memorypar,fill=memorypar))  +        facet_wrap(~process,nrow=2) +        geom_point(position=position_jitterdodge(dodge.width=0.9)) +       geom_boxplot(fill="white",outlier.colour = NA,                          position = position_dodge(width=0.9))



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!