[R] Parliament Seats Graph

Duncan Murdoch murdoch.duncan at gmail.com
Fri Sep 12 13:41:43 CEST 2014

On 12/09/2014, 7:18 AM, Barry Rowlingson wrote:> On Fri, Sep 12, 2014 at
11:25 AM, Jim Lemon <jim at bitwrit.com.au> wrote:
>> I can see how you would plot the points going from right to left (the
>> way), by plotting the next point on the arc with the least increase in
>> angle from the last point plotted. If this is the way you have worked
out, I
>> think all that you have to do is to turn the party affiliation into a
factor (if
>> it is not already) and plot the points by the sorted numeric value of the
>> factor. You will probably want to adjust the levels to some political
>> dimensions before doing the sort.
>  I'm interested in how you get exactly N seats in M rows that look as
> neat as that. My eyes are going funny trying to count the dots in each
> arc but there must be some nice algorithm for generating a sequence
> that sums to N, has M elements, and has a small variable difference
> between the row sizes to constrain the sum...
>  Or am I overthinking this?

I would guess it's something like this:

1.  Set the radii of each arc.  Since the spacing of the dots looks
even, the number of dots in each arc should be proportional to the radius.

2.  Using the proportions above find the number of dots in the largest
arc by rounding the proportion times total to an integer.

3.  Repeat for each arc moving inwards, subtracting the number of dots
already shown from the grand total.

The same scheme can be used to set the number of dots of each colour in
each arc.

For example:

radii <- seq(2.5, 1, len=11)
N <- 449
counts <- numeric(11)
plot(c(-2.5, 2.5), c(0, 2.5), type="n", axes=FALSE, asp=1)
for (i in 1:11) {
  counts[i] <- round(N*radii[i]/sum(radii[i:11]))
  theta <- seq(0, pi, len = counts[i])
  points(radii[i]*cos(theta), radii[i]*sin(theta))
  N <- N - counts[i]

Duncan Murdoch

More information about the R-help mailing list