How to make the jitter point centered using ggplot2?

回眸只為那壹抹淺笑 提交于 2019-12-06 04:49:57

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?

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

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