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)
The function geom_point() adds a layer of points to your plot, which creates a scatterplot.
geom_point.Rd. The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With