How to make the jitter point centered using ggplot2?

别来无恙 提交于 2019-12-22 14:41:39

问题


I am trying to recreate a multiple dot plot like the figure below. https://www.dropbox.com/s/9jqguesqd5gdm99/jitter%20plot.png

Both geom_dotplot and position_jitter in ggplot2 have been tried. But neither of these two commands could make it.

The figure created by position_jitter is quite similar, but there are also some differences. The point created by position_jitter seems too scattered, compared to the figure mentioned above.

This is the figure I could make so far. https://www.dropbox.com/s/athsgkjjrlwr15k/figure.png?dl=0 e.g.

value<-c(141573,  262616,   66773.8,    93032.2,    55528.8,    113125, 252954, 275581, 207854, 183300, 292946, 171510, 343565, 214436, 295871, 187196, 207352, 180356, 158110, 241769, 180112, 194007, 529168, 229267, 344257, 337311, 255109, 307389, 416108, 405033, 292260, 354368, 416811, 330420, 353017, 333997, 389285, 289870, 289224, 401641, 206481, 367379)
group<-factor(c(1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2))
quantile<-boxplot(value~group)
line<-quantile$stats
library(ggplot2)
gg<-ggplot()+geom_point(aes(x=group,y=value,color=group,fill=group),position=position_jitterdodge(jitter.width=0.3,dodge.width=0.7),size=4)+
geom_path(aes(x=c(0.8,1.2),y=c(line[3,1],line[3,1])),size=1)+geom_path(aes(x=c(1.8,2.2),y=c(line[3,2],line[3,2])),size=1)+geom_path(aes(x=c(0.9,0.9,1.1,1.1),y=c(line[2,1],line[2,1],line[2,1],line[2,1])),size=1.2)+geom_path(aes(x=c(0.9,0.9,1.1,1.1),y=c(line[4,1],line[4,1],line[4,1],line[4,1])),size=1.2)+geom_path(aes(x=c(1.9,1.9,2.1,2.1),y=c(line[2,2],line[2,2],line[2,2],line[2,2])),size=1.2)+geom_path(aes(x=c(1.9,1.9,2.1,2.1),y=c(line[4,2],line[4,2],line[4,2],line[4,2])),size=1.2)+geom_path(aes(x=c(1,1),y=c(line[2,1],line[4,1])),size=1.2)+geom_path(aes(x=c(2,2),y=c(line[2,2],line[4,2])),size=1.2)+
scale_colour_manual(name="",values = c("1"="#353F6B","2"="#994642"))+
theme_classic()+theme(legend.position="none")
gg

So my question: When using jitter function in ggplot2, is it possible to make outliers center, just like the first figure. And what kind of method would you suggest for creating this kind of figure?

Thanks


回答1:


In ggplot2, the 'jitteriness' is configurable in both directions, e.g.

data(mpg)
ggplot(mpg, aes(x=cyl, y=hwy, group=factor(cyl))) +
    geom_boxplot() +
    geom_jitter(position = position_jitter(height = .2, width = .2))

... looks like this:

whereas:

ggplot(mpg, aes(x=cyl, y=hwy, group=factor(cyl))) +
    geom_boxplot() +
    geom_jitter(position = position_jitter(height = .7, width = .7))

... has a more 'shotgun spray' feel to it:

Could you create the effect you're looking for by tweaking the position parameter in geom_jitter?




回答2:


Seems you only need to adjust jitter.width. I added alpha into the code. See # <<--

gg<- ggplot()+
  geom_point(aes(x=group,y=value,color=group,fill=group), 
             position=position_jitterdodge(jitter.width=0.1,  # <<- adjusted
                                           dodge.width=0.7), 
             size=4, alpha=0.7)+                              # <<- alpha added
  geom_path(aes(x=c(0.8,1.2),y=c(line[3,1],line[3,1])),size=1)+
  geom_path(aes(x=c(1.8,2.2),y=c(line[3,2],line[3,2])),size=1)+
  geom_path(aes(x=c(0.9,0.9,1.1,1.1),y=c(line[2,1],line[2,1],line[2,1],line[2,1])),size=1.2)+
  geom_path(aes(x=c(0.9,0.9,1.1,1.1),y=c(line[4,1],line[4,1],line[4,1],line[4,1])),size=1.2)+
  geom_path(aes(x=c(1.9,1.9,2.1,2.1),y=c(line[2,2],line[2,2],line[2,2],line[2,2])),size=1.2)+
  geom_path(aes(x=c(1.9,1.9,2.1,2.1),y=c(line[4,2],line[4,2],line[4,2],line[4,2])),size=1.2)+
  geom_path(aes(x=c(1,1),y=c(line[2,1],line[4,1])),size=1.2)+
  geom_path(aes(x=c(2,2),y=c(line[2,2],line[4,2])),size=1.2)+
  scale_colour_manual(name="",values = c("1"="#353F6B","2"="#994642"))+
  theme_classic()+theme(legend.position="none")
gg

Just a side note: I'm using ggplot2 1.0.0



来源:https://stackoverflow.com/questions/26193434/how-to-make-the-jitter-point-centered-using-ggplot2

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