This is a solution based on interval operations.

### Usage and examples

First, let's look at how to use the function. The code is at the end.

Let's generate some sample data and plot it:

```
data1 = RandomVariate[ExponentialDistribution[1], 200];
data2 = RandomVariate[NormalDistribution[2, 1], 200];
beeswarmPlot[data1]
```

Now let's plot two together:

```
beeswarmPlot[{data1, data2}]
```

We can also specify the circle radius explicitly, in plot coordinates:

```
beeswarmPlot[data2, 0.2]
```

Or we can change the colour while keeping the radius selection automatic:

```
apricot = RGBColor[1.`, 0.340007`, 0.129994`];
cornflower = RGBColor[0.392193`, 0.584307`, 0.929395`];
beeswarmPlot[{data1, data2}, Automatic, PlotStyle -> {apricot, cornflower}]
```

### The code

Note: I'm going for readability here, not performance. Performance can be improved significantly at the cost of readability, which is already impaired by the large amount of code used just for option handling.

I am going to use these helper functions:

```
intervalInverse[Interval[]] := Interval[{-Infinity, Infinity}]
intervalInverse[Interval[int__]] :=
Interval @@ Partition[
Replace[Flatten[{int}],
{{-Infinity, mid___, Infinity} :> {mid},
{-Infinity, mid__} :> {mid, Infinity},
{mid__, Infinity} :> {-Infinity, mid},
{mid___} :> {-Infinity, mid, Infinity}
}
], 2]
intervalComplement[a_Interval, b__Interval] :=
IntervalIntersection[a, intervalInverse@IntervalUnion[b]]
```

This is the code for calculating the point coordinates and packing the circles. This is the only function that needs to be changed to implement an different packing method.

```
(* data is assumed to be a sorted vector of numbers *)
beeswarm[data_, radius_] :=
Module[{points, left, right, int},
points = {};
Do[
int = Interval @@ Cases[points, {x_, y_} /; y > pt - radius :> x + {-1, 1} Sqrt[radius^2 - (pt - y)^2]];
right = Min[intervalComplement[Interval[{0, Infinity}], int]];
left = Max[intervalComplement[Interval[{-Infinity, 0}], int]];
AppendTo[points, {If[right < -left, right, left], pt}],
{pt, data}
];
points
]
```

And this is the plotting function that provides a user friendly interface (option handling) and assembles the final `Graphics`

object.

```
Options[beeswarmPlot] =
Join[
Options[Graphics],
{PlotStyle -> Automatic}
];
SetOptions[beeswarmPlot, Frame -> True];
SetOptions[beeswarmPlot, FrameTicks -> {None, Automatic}];
beeswarmPlot[data_?(VectorQ[#, NumericQ] &), radius : (_?NumericQ | Automatic) : Automatic, opt : OptionsPattern[]] := beeswarmPlot[{data}, radius, opt]
beeswarmPlot[data : {__?(VectorQ[#, NumericQ] &)}, radius : (_?NumericQ | Automatic) : Automatic, opt : OptionsPattern[]] :=
Module[{r, order, flatData, colours, colfun},
(* generate colour indices and sort them together with the data *)
flatData = Flatten[data];
order = Ordering[flatData];
colours = Flatten@Table[ConstantArray[i, Length[data[[i]]]], {i, Length[data]}];
flatData = flatData[[order]];
colours = colours[[order]];
(* automatic radius selection *)
r = If[radius === Automatic, 4 Mean@Differences[flatData], 2 radius];
(* handle the PlotStyle option *)
colfun = With[
{ps = OptionValue[PlotStyle]},
Switch[ps,
Automatic, ColorData[1],
_List, Function[i, ps[[ Mod[i, Length[ps], 1] ]] ],
_, ps &
]
];
(* call the packing function and build the graphics using the result *)
Graphics[
MapThread[{colfun[#2], Disk[#1, 0.95 r/2]} &, {beeswarm[flatData, r], colours}],
Sequence @@ FilterRules[{opt}, Options[Graphics]],
Frame -> OptionValue[Frame],
FrameTicks -> OptionValue[FrameTicks]
]
]
```

I suggest you start by looking at these Q&As: http://mathematica.stackexchange.com/questions/tagged/packing

– Mr.Wizard – 2014-02-19T22:25:28.650Also, and perhaps more relevant: http://mathematica.stackexchange.com/q/2594/121

– Mr.Wizard – 2014-02-19T22:26:59.3605If you concisely explain what is a "beeswarm plot", it'll increase your chances of getting an answer. People are less likely to become interested if they have to look up various R functions just to understand the question. The page you linked to is not too clear to someone unfamiliar with R or its "stripchart". – Szabolcs – 2014-02-19T22:34:10.657

A Beeswarm plot is a 1-D scatter plot: with two special properties: points are represented as non-overlapping circles and circles (data markers) are closely packed – Pam – 2014-02-19T23:29:12.063

@Pam Please include that in the question body itself. Also, directly imbedding an image or two as examples would be helpful. – Mr.Wizard – 2014-02-19T23:41:13.047

1Sorry, I downvoted temporarily until a reasonable explanation of a Beeswarm plot is included in the question – Dr. belisarius – 2014-02-20T00:10:10.727

@Pam Please also explain what sort of data is used to generate such a plot, and how it's used exactly. Is it the circle coordinates? – Szabolcs – 2014-02-20T00:13:18.847

1

may be you can use RLink to call that R function from M? https://reference.wolfram.com/mathematica/RLink/guide/RLink.html and do you do not have to have an implementation in M for it.

– Nasser – 2014-02-20T00:20:42.0201

I think this could have been one of those 20+ upvoted questions with several interesting implementations, if only you had put in a little effort to make the question clear and actually

– Szabolcs – 2014-02-20T16:41:00.540attractsome answers. People love graphics. Remember that the answers are only as good as the question, and the answers will only have as much effort put into them as the question itself. It's a big missed opportunity.Szabolcs: See my edits. I hope this is sufficient. – Pam – 2014-02-20T21:15:32.633

@Pam Yes, it's much better now. – Szabolcs – 2014-02-20T21:48:33.907

1In the bottom picture, are the "normal" and the "uniform" switched? – bill s – 2014-02-20T23:43:59.350

Ok, removing my downvote an upvoting. Great work. – Dr. belisarius – 2014-02-21T00:57:58.317

@bills They're definitely switched. – Szabolcs – 2014-02-21T02:27:25.617