Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using scale_size_area (ggplot2) to plot points of size "0" as completely absent

Tags:

plot

r

ggplot2

I'm trying to make a bubble plot of condition by cluster, where the size of each bubble is set by a third "percent" variable. As per the ggplot2 documentation, I think I should be able to do this via scale_size_area. I am unclear why this isn't working, and I still see very tiny points when percent=0. (If I am misunderstanding, I would also appreciate a solution on how to do this. In my real data, it is important to distinguish between 0 and very close to 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

like image 385
blep Avatar asked Jun 07 '16 19:06

blep


People also ask

What does Geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.

What does Geom_point mean?

geom_point.Rd. The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.

Can you use Ggplot without a Dataframe?

Using ggplot2 with a matrixggplot only works with data frames, so we need to convert this matrix into data frame form, with one measurement in each row. We can convert to this “long” form with the melt function in the library reshape2 .


1 Answers

Try

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)

Using size=ifelse(Percent==0, NA, Percent)) instead of size=Percent will exclude those points from drawing.

like image 90
lukeA Avatar answered Nov 13 '22 07:11

lukeA