Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: plotting two size aesthetics

Tags:

r

ggplot2

From what I can find on stackoverflow, (such as this answer to using two scale colour gradients on one ggplot) this may not (yet) be possible with ggplot2.

I want to create a bubbleplot with two size aesthetics, one always larger than the other. The idea is to show the proportion as well as the absolute values. Now I could colour the points by the proportion but I prefer multi-bubbles. In Excel this is relatively simple.Excel multiple aesethetic example on mtcars (https://i.sstatic.net/v5LsF.png) Is there a way to replicate this in ggplot2 (or base)?

like image 430
Hugh Avatar asked Mar 05 '26 09:03

Hugh


1 Answers

Here's an option. Mapping size in two geom_point layers should work. It's a bit of a pain getting the sizes right for bubblecharts in ggplot though.

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(size = disp), shape = 1) +
  geom_point(aes(size = hp/(2*disp))) + scale_size_continuous(range = c(15,30))

To get it looking most like your exapmle, add theme_bw():

P <- p + theme_bw()

The scale_size_continuous() is where you have to just fiddle around till you're happy - at least in my experience. If someone has a better idea there I'd love to hear it. enter image description here

like image 137
alexwhan Avatar answered Mar 07 '26 23:03

alexwhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!