[R] Parliament Seats Graph
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Fri Sep 12 15:35:46 CEST 2014
Note if you are trying to compare parliament diagrams created with
this code with images from wikipedia, the wikipedia images I tried are
wrong.
The Ukrainian one: http://en.wikipedia.org/wiki/Verkhovna_Rada shows
two groups in red but the legend only has one red party, and the
French Senate: http://en.wikipedia.org/wiki/French_Parliament has 21
red "Communist" dots but the text says 20. It also has 10 green
("Green") dots but the text says 12.
Maybe wikipedia would like to use this code to generate these diagrams
from the data!
On Fri, Sep 12, 2014 at 1:01 PM, Rowlingson, Barry
<b.rowlingson at lancaster.ac.uk> wrote:
> 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
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list