plot with overlayed circles in ggplot2

176 views
Skip to first unread message

deeps...@gmail.com

unread,
Feb 26, 2014, 2:56:12 PM2/26/14
to ggp...@googlegroups.com
Hello,

I am using ggplot2 (code is below) to create a plot with overlayed circles similar to here (mine is even simpler):

http://rgraphgallery.blogspot.com/2013/04/rg-heatmap-with-overlayed-circle-size.html

I plot 36 circles where the size of the circles is dictated by the values in the matrix "prop_sel_var1" and the colors are determined by the values in the matrix "avg_est_var1." The question I have is regarding the size of the circles and trying to place a black edge around these circles.

Since I use the following code with "max_size" set to 20

scale_size_area(breaks=seq(0.3,1,length.out=4), max_size=20)

and the largest value in "prop_sel_var1" is 1, I had assumed that the diameter of the largest circle (i.e. those with values of 1 in prop_sel_var1) would be 5.046265- I determined this by using the area of a circle and solving for the radius when area is 20 (and then multiplying the computed radius by 2 in order to get the diameter), i.e. 2 * (20/pi)^0.5. And I also thought that another entry in the matrix "prop_sel_var1", for example 0.336 in element [1,2], would be represented by a circle with area equal to 0.336 times the area of the largest cicle (i.e. 0.336*20). Does anyone know if this is right?

To get a black edge around the circles, I used the syntax

geom_point(pch=21, size=5.046265)

where "size" is supposed to be set to the diameter of the desired point according to here

http://sape.inf.usi.ch/quick-reference/ggplot2/geom_point

After doing this I was expecting to find a black edge at least around the largest circles, but instead the black circular edge was considerably smaller for these largest circles. Does anyone know what I am doing wrong?

Thanks,
Sandeep

R Code:

library(ggplot2)

K=6
prop_sel_var1=matrix(nrow=K, c(1.000, 0.300, 0.306, 1.000, 0.264, 0.330, 0.336, 0.336, 0.310, 0.324, 0.290, 0.326, 0.304, 0.340, 0.298, 0.356, 1.000, 0.336, 0.344, 0.998, 0.278, 0.310, 0.292, 0.304, 0.298, 0.336, 0.990, 0.328, 0.314, 0.320, 0.414, 0.332, 0.294, 0.342, 0.310, 1.000))

avg_est_var1=matrix(nrow=K, c(0.7032789792, 0.0092935272, -0.0049142134, 0.5226539595, -0.0022733636, -0.0009967606, 0.010058062, -0.003814100, -0.001076480, 0.008751295, 0.001249062, -0.004649073, 0.0019329838, 0.0003191083, -0.0018377246, 0.0021648795, 0.5060364430, -0.0014738496, 0.013375824, 0.212841136, -0.001722890, 0.012628489, -0.003604888, -0.005342961,0.0023828353, 0.0006147270, -0.2145598261, -0.0005725531, -0.0007588590, -0.0016428822, 0.0036059260, -0.0008337600, 0.0001128477, 0.0020181010, -0.0019468195, 0.7138050152))

rect = rep(0,36)
circlefill = as.numeric(avg_est_var1)
circlesize = as.numeric(prop_sel_var1)

myd <- data.frame(rowv = rep(1:K, K), columnv = rep(1:K, each=K),circlesize, circlefill)

p <-  ggplot(myd, aes(y=rev(factor(rowv)), x=factor(columnv)))
p +
geom_tile(aes(fill = rect)) +
scale_fill_continuous(low = "white", high = "white") +
guides(fill=FALSE) +
geom_point(aes(colour=circlefill, size =circlesize)) +
scale_colour_gradientn(colours=c("lightblue2","white","orangered3","darkred")) +
guides(size = guide_legend(override.aes = list(shape = 1))) +
#scale_size(breaks=seq(0.3,1,length.out=4), range=c(8,20)) +
scale_size_area(breaks=seq(0.3,1,length.out=4), max_size=20) +
geom_point(pch=21, size=5.046265) +
scale_x_discrete(breaks=c(1,2,3,4,5,6), labels=c("Y1(t)","Y2(t)","Y3(t)","Y4(t)","Y5(t)","Y6(t)"), name="") +
scale_y_discrete(breaks=c(1,2,3,4,5,6), labels=c("Y6(t)","Y5(t)","Y4(t)","Y3(t)","Y2(t)","Y1(t)"), name="") +
theme_bw() +
theme(legend.title=element_blank())

Ben Bond-Lamberty

unread,
Feb 26, 2014, 3:33:09 PM2/26/14
to ggplot2
Kind of confusing that you have two separate geom_point calls; also
you're setting color and fill both equal to circlefill, which I don't
think you want to do for an always-black ring.

Simplifying things for now...does this work for you?

ggplot(myd,aes(rowv,columnv))+geom_point(pch=21,aes(size=circlesize,fill=circlefill))+scale_size_area(breaks=seq(0.3,1,length.out=4),
max_size=20)

Regards
Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

deeps...@gmail.com

unread,
Feb 27, 2014, 5:49:11 PM2/27/14
to ggp...@googlegroups.com
Ben,

Thanks! That's very close to what I want (particularly with the black border around the cirlces). I was wondering if there is any way to add my original color scheme for the circles


scale_colour_gradientn(colours=c("lightblue2","white","orangered3","darkred"))

Also, my other main question was how to interpret the (relative) sizes of these circles.

Since the largest value in the matrix "prop_sel_var1" is 1 and max_size is set to 20, is it the case that the diameter of the largest circle is 5.0463, i.e. 2 * (20/pi)^0.5 (using the area of a circle and solving for the radius when area is 20). Also, is it the case that the diameter of the circle represented by another element in prop_sel_var1 such as 0.336, which is element [1,2] in the matrix prop_sel_var1, has area of 0.336*20=6.72 and diameter of  2 * (6.72/pi)^0.5?

Thanks again for all of your help,
Sandeep

Ben Bond-Lamberty

unread,
Feb 28, 2014, 11:35:39 AM2/28/14
to ggplot2
Re the color scheme, can you just add
+scale_fill_gradientn(colours=c("lightblue2","white","orangered3","darkred"))
?

I don't know for sure about the size question, but your assumption
makes sense to me.
Ben

deeps...@gmail.com

unread,
Mar 3, 2014, 12:57:44 PM3/3/14
to ggp...@googlegroups.com
Ben,

That worked. Thanks!

Sandeep
Reply all
Reply to author
Forward
0 new messages