3

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.stack.imgur.com/v5LsF.png) Is there a way to replicate this in ggplot2 (or base)?

Community
  • 1
  • 1
Hugh
  • 15,521
  • 12
  • 57
  • 100
  • can you please provide the data so that this is a reproducible example? – alexwhan Jun 24 '13 at 02:22
  • Sorry, it's `mtcars` with `mpg` on the x-axis, `wt` on the y-axis. The sizes are `hp/2` (black) and `disp` (white). – Hugh Jun 24 '13 at 02:25

1 Answers1

5

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

alexwhan
  • 15,636
  • 5
  • 52
  • 66
  • Argh, I'd forgotten to enclose `size = ` with `aes()`! Thank you! – Hugh Jun 24 '13 at 03:19
  • How do I implement that @agstudy? I've tried `p + theme(theme_bw())` – Hugh Jun 24 '13 at 04:52
  • @flodel - sure, for plotting questions I usually try to restrict discussion down the very basics of the concept - hopefully it is less confusing. But `theme_bw` no doubt gets clost to the OP's original plot – alexwhan Jun 24 '13 at 04:59
  • and how can we label both variables?? – sheriff Aug 31 '17 at 09:52