# How do I create an ellipse bubble chart?

I have some data that I would like to plot as a bubble chart. The data is formatted as {{val1_mean,val1_sd,val2_mean,val2_sd},{val1_mean,......}}. What I would like to do is plot a bubble chart where the x/y location of the bubble is val1_mean,val2_mean and the width/height of the bubble is val1_sd,val2_sd.

So far I've been able to do circular bubble charts with the inbuilt function but am a bit lost as to how to make the bubbles ellipses instead of circles.

Here's the code I've written so far;

bubleData={{1.88308*10^6, 1024.85, 149446., 6636.62}, {1.98345*10^6, 15022.5,
126966., 7071.75}, {1.94677*10^6, 9281.83, 131930.,
7435.15}, {1.88308*10^6, 1024.85, 127423., 8718.36}};
BubbleChart[bubbleData[[All, 1 ;; 3]],ChartStyle -> Directive[Opacity[0.5]]]


-

To use BubbleChart as you have indicated in your comment to Jonathan's answer, a bit of work is required. First, BubbleChart expects data of the form:

{x, y, w}


So, we need to turn your data into something like that, but we need to take a pointer from the documentation to include all of the data. To do that, we add the extra information as metadata:

{#1, #2, 1} -> {##3}& @@@ bubbledata


which returns

{{1.88308*10^6, 1024.85, 1} -> {149446., 6636.62},
{1.98345*10^6, 15022.5, 1} -> {126966., 7071.75},
{1.94677*10^6, 9281.83, 1} -> {131930., 7435.15},
{1.88308*10^6, 1024.85, 1} -> {127423., 8718.36}}


where the 1 acts as a placeholder. Now we create a custom ChartElementFunction:

f[_, v_, {meta_}, ___] := Disk[Most @ v, meta]


where I determined from experimentation that the third parameter is the data point itself, so we strip off the last element. Putting it all together,

BubbleChart[{#1, #2, 1} -> {##3}& @@@ bubbledata,
ChartElementFunction -> f,
PlotRange -> {{1.70*^6, 2.15*^6}, {-10000, 23000}},
PerformanceGoal -> "Speed"
]


gives

Two things to note about the final code:

1. The PlotRange required a lot of manual adjustment.
2. I used PerformanceGoal -> "Speed" to eliminate the mouseover effects that are normally there as they only show the placeholder I used, so they are not that useful here.
-
Many thanks @rcollyer. I spent a while experimenting with ChartElementFunction but didn't quite get to that point. – Jonathan Shock Apr 29 '13 at 3:20
@JonathanShock Thanks. I hadn't thought of using ChartElements, so I think we're even. :) – rcollyer Apr 29 '13 at 3:23
I'm getting a message "f is not a Graphics primitive or directive." in Mathematica 9.0.1.0 – Cam Apr 29 '13 at 4:22
@Cam, me too, with 9.0.0.0. – Jonathan Shock Apr 29 '13 at 6:23
@Cam I was to, for a bit on 9.0.1, and I don't remember how I solved it. I'll look at it in a bit. – rcollyer Apr 29 '13 at 11:35

It seems to be possible with ChartElementsFunction but by default BubbleChart just takes 3 arguments so you would have to play with this option. To do precisely what you want it seems easier to define your own graphics object like this:

ellipse[v_]:=Disk[{v[[1]],v[[2]]},{v[[3]]/10,v[[4]]/10}]
datatoplot=RandomReal[1, {10, 4}];
blankplot=ListPlot[{0, 0},PlotStyle->White,PlotRange->{{-0.1, 1.1},{-0.1,1.1}}];
Show[blankplot, Graphics[Style[ellipse[#], Blue] & /@ datatoplot]]


and you'll get something like this:

Edit:

(Edit 2: The following is not currently correct. As pointed out by @Cam, the ChartElements are not showing up with the correct AspectRatio)

It was pointed out that there are some features of BubbleChart which are desirable, so I'll suggest the following:

datatoplot = RandomReal[1, {10, 4}];
ellipse[v_] := Disk[{v[[1]], v[[2]]}, {v[[3]]/10, v[[4]]/10}];
ce=Show[Graphics[Style[ellipse[#], Blue]]] & /@ datatoplot;
Show[BubbleChart[{datatoplot[[;; , ;; 3]][[#]]},ChartElements -> ce[[#]]] & /@ Range[Length[datatoplot]], PlotRange -> All]


Which should allow you to define the ellipses as you want and plot each ChartElement individually. In this case there are as many BubblePlots as there are datapoints and they are all then shown together. This may not be the most efficient way but it was the best way I found to have the ChartElements show up correctly.

This produces the plot below (plot points differ from the above as they are randomly generated):

-
Thanks Jonathan. If at all possible I'd like the keep the bubble chart. I'm currently using a tool tip so when you place your mouse over the bubble a table appears showing the values (I left that bit out of the question because I didn't want to complicate things). The bubble chart also highlights the bubble you're mouse is over which looks nice. – Cam Apr 29 '13 at 2:07
Fair enough. I've written an alternative version which should include what you want. – Jonathan Shock Apr 29 '13 at 3:09
Incidentally, your use of Show is not necessary as you are not combining Graphics objects. – rcollyer Apr 29 '13 at 3:22
Does this solution mean I have to scale my data? – Cam Apr 29 '13 at 4:27
@Cam, that depends. If the data are between 0 and 1 and the standard deviations take on a large range of values over that range then the ellipses are likely to overlap. This is a matter of the nature of the data. If this were a problem you might think about decreasing the Opacity of the Graphics objects. @rcollyer, I have a Table of BubbleCharts as I'm setting it up as BubbleChart[data[[#]],ChartElement->ce[[#]]]&/@Range[data] so I used Show. I'm sure there is a way to include this all in one BubbleChart, but this was the easiest way I found to present it. – Jonathan Shock Apr 29 '13 at 4:42