2015-10-19 54 views
0

我想用ggplot到archieve以下情节类型点:绘制每n观察

sketch of a ggplot showing one dot for every 10 observations

使用下面的数据:

t <- read.table(header=T, row.names=NULL, 
    colClasses=c(rep("factor",3),"numeric"), text= 
"week team level n.persons 
1  A  1  50 
1  A  2  20 
1  A  3  30 
1  B  1  50 
1  B  2  20 
2  A  2  20 
2  A  3  40 
2  A  4  20 
2  B  3  30 
2  B  4  20") 

到目前为止,通过应用这种转变

t0 <- t[ rep(1:nrow(t), t$n.persons %/% 10) , ] 

and plotting

ggplot(t0) + aes(x=week, y=level, fill=team) + 
geom_dotplot(binaxis="y", stackdir="center", 
      position=position_dodge(width=0.2) 

我可以生成

my archievement in ggplot so far: dots do not dodge each other vertically

答:如何archieve不同团队的垂直点闪避相互不重叠?

B:有没有可能是点的全包始终处于居中,即 如果在一个地方只有一支球队的点没有发生闪避?

+0

我给这里的气泡图表类似的解决方案:http://stackoverflow.com/questions/26757026/bubble-chart-with-ggplot2/26769996#26769996 geom_jitter可能是你在这里寻找。它会移动点。结合alpha透明度,它可以提供一个工作解决方案。 – Docconcoct

回答

2

下面的代码将停止重叠:

t0 <- t[ rep(1:nrow(t), t$n.persons %/% 10) , ] 

t0$level <- as.numeric(t0$level) # This changes the x-axis to numerics 

t0$level <- ifelse(t0$team == "B", (t0$level+.1), t0$level) # This adds .1 to the position on the x-axis if the team is 'B' 

ggplot(t0) + aes(x=week, y=level, fill=team) + geom_dotplot(binaxis="y", stackdir="center", 
      position=position_dodge(width=0.2)) 

这里是输出:

enter image description here

你也减去值向下移动的点,如果你更希望。

如果你想行完全点之间这段代码应该这样做:

t0$level <- ifelse(t0$team == "B", (t0$level+.06), t0$level) 
t0$level <- ifelse(t0$team == "A", (t0$level-.06), t0$level) 

输出:

enter image description here

我不知道把我的头顶部如何当给定坐标上只有一个团队时,跳过上面的ifelse。我猜想你需要在每个坐标处统计唯一的团队标签,并且只有当该计数> 1时,才能运行上面的代码。

+1

谢谢,这似乎离我最近。好像我必须深入了解如何编写自己的geoms和aestetics的文档:-) – akraf