[R] Parliament Seats Graph

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Sep 12 14:01:57 CEST 2014


I've generalised Duncan's code:

seats <- function(N,M, r0=2.5){
    radii <- seq(r0, 1, len=M)

    counts <- numeric(M)
    pts = do.call(rbind,
            lapply(1:M, function(i){
        counts[i] <<- round(N*radii[i]/sum(radii[i:M]))
        theta <- seq(0, pi, len = counts[i])
        N <<- N - counts[i]
        data.frame(x=radii[i]*cos(theta), y=radii[i]*sin(theta), r=i,
theta=theta)
    }  )
            )
    pts = pts[order(-pts$theta,-pts$r),]
    pts
}


and written this:

election <- function(seats, counts){
    stopifnot(sum(counts)==nrow(seats))
    seats$party = rep(1:length(counts),counts)
    seats
}


sample usage:

> layout = seats(449,16)
> result = election(layout, c(200,200,49)) # no overall majority!!!
> plot(result$x, result$y, col=result$party,pch=19, asp=1)

Looks like a start...


On Fri, Sep 12, 2014 at 12:41 PM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> 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
> easy
>>> 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