2016-06-07 419 views
0

我试图通过聚簇来创建条件的气泡图,其中每个气泡的大小由第三个“百分比”变量设置。至于per the ggplot2 documentation,我想我应该可以通过scale_size_area来做到这一点。我不清楚为什么这不起作用,当百分比= 0时,我仍然看到非常微小的点。使用scale_size_area(ggplot2)绘制大小为“0”的点为完全不存在

ex <- data.frame(Condition=rep(c("ex1","ex2","ex3","ex4"),4), 
       Cluster=c(rep(1,4),rep(2,4),rep(3,4),rep(4,4)), 
      Percent=c(0,0,0,1,0.25,0,0.25,0.5,1,0,0,0,0.25,0.25,0.25,0.25)) 
ggplot(ex, aes(Cluster, Condition, size=Percent))+ 
      geom_point(color = "blue")+ scale_size_area(max_size=20) 

example plot

+1

E.g. 'size = ifelse(Percent == 0,NA,Percent))'而不是'size = Percent'? – lukeA

+1

它可能与'stroke'(点的轮廓)有关。虽然我的印象是,只有21点到25点有一个轮廓,当'百分号'为0时,在'geom_point'中使用'stroke = 0'将删除点。 – aosmith

+0

@lukeA工作!如果您想将其作为答案发布,我很乐意接受它。 – dd3

回答

2

尝试(如果我的误解,我还想了解如何做到这一点的解决方案。在我的真实数据,它0,非常接近0区分是很重要的)

library(ggplot2) 
ex <- data.frame(Condition=rep(c("ex1","ex2","ex3","ex4"),4), 
       Cluster=c(rep(1,4),rep(2,4),rep(3,4),rep(4,4)), 
      Percent=c(0,0,0,1,0.25,0,0.25,0.5,1,0,0,0,0.25,0.25,0.25,0.25)) 
ggplot(ex, aes(Cluster, Condition, size=ifelse(Percent==0, NA, Percent))))+ 
      geom_point(color = "blue")+ scale_size_area(max_size=20) 

使用的size=ifelse(Percent==0, NA, Percent))代替size=Percent会从图纸排除那些点。

0

你也可以尝试以下方法,使用data.table:

library(data.table) 
ex2<-as.data.table(ex) 
ggplot(ex2, aes(Cluster, Condition))+ 
geom_point(data=ex2[ex2$Percent > 0],aes(size=Percent), color = "blue")+scale_size_area(max_size=20) 

在这里,你只需创建排除与平等的百分之所有行geom_point内的新的帧0 的问题的办法之前,就是:如果所有行都具有百分之等于零,说

Percent=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) 

你会得到一个错误

错误grid.Call.graphics(C_setviewport,副总裁,TRUE): nicht-endlicher的Ort奥德/ UND GROSSE DES视口

如果您使用的数据表的方式(或任何其他,让ggplot刚你想要绘制的信息),你没有任何问题,你会得到一个空的情节,如果是自动化脚本比崩溃更好。