2016-03-15 55 views
1

重叠我沃尔德想做一个情节GGPLOT2与价值观相一致的中心,但在重叠躲闪,像这样的(在GraphPad Prism中进行)抖动躲闪或仅在GGPLOT2

plot in graphpad prism

什么我可以做GGPLOT2抖动看起来像这样

df<-data.frame(response=c(-0.3502294,0.4207441,0.1001638,-0.2401336,-0.2604142,0.4574286, 
     0.755964,0.9241669,0.8212376,2.037581,0.6440635,0.2714898,1.433149,0.4627742, 
     0.5639637,0.1610219,0.1516505,-1.322015,-0.2134711,0.8554756,0.400872,1.344739, 
     0.3743637,0.6329151,0.1467015,0.6313575,0.3989693,0.1940468,-0.06594919,-0.1951204), 
    group=c(rep("A",10),rep("B",10),rep("C",10))) 

set.seed(1234) 
ggplot(df,aes(group,response,fill=group))+ 
    geom_point(size=5,pch=21,position=position_jitter(w=.2))+ 
    scale_y_continuous(limits=c(-2,3)) 

plot in R with ggplot2

我怎样才能保持点对齐的中心,闪避只有在ggplot2重叠?

回答

1

我对你的问题的解决方案是:

  1. 将数据分成重叠和非重叠点。这可以通过计算以前各点之间的差异来完成。如果它低于某个阈值,那么该点与某点重叠,因此应该应用一些抖动,同时绘制该点。否则,这个点就是这样绘制的。
  2. 使用geom_point分别绘制两个数据。

以下是使用上述逻辑的代码。

library(data.table) 

threshold <- 0.1 

df<-data.frame(response=c(-0.3502294,0.4207441,0.1001638,-0.2401336,-0.2604142,0.4574286, 
     0.755964,0.9241669,0.8212376,2.037581,0.6440635,0.2714898,1.433149,0.4627742, 
     0.5639637,0.1610219,0.1516505,-1.322015,-0.2134711,0.8554756,0.400872,1.344739, 
     0.3743637,0.6329151,0.1467015,0.6313575,0.3989693,0.1940468,-0.06594919,-0.1951204), 
    group=c(rep("A",10),rep("B",10),rep("C",10))) 

df1 <- data.table(df[order(df$group, df$response),]) 
df1[, diffFromLast:=response - shift(response, n=1, type="lag"), by=group] 
nonZitterPoints <- df1[ is.na(diffFromLast) | (diffFromLast > threshold),] 
zitterPoints <- df1[ which(diffFromLast < threshold),] 

g1 <- ggplot()+ 
     geom_point(data=nonZitterPoints,aes(group,response,fill=group), size=5,pch=21)+ 
     scale_y_continuous(limits=c(-2,3)) 
g1 

g2 <- g1 + geom_point(data=zitterPoints,aes(group,response,fill=group), size=5,pch=21, position=position_jitter(w=.2)) 

g2 
+0

这是回答您的问题@RBA? –

+0

这是一个可能的解决方案,但它不是我要找的。我想要重叠(并且抖动)的点之一居中,并且您的解决方案都会到达抖动所需的位置。必须有一种方法来排名他们......但无论如何感谢@Kumar – RBA