Re: [R] Parliament Seats Graph
On Mon, 8 Sep 2014 10:22:03 PM Stefan Petersson wrote: Hi, Is there any package (or homegrown function) that can produce Parliament Seats Graph? I'm referring to the nice looking concentric half circles of colored seats as seen on Wikipedia (for example). I can pretty easily plot the points and color them. But I can't group the colored points in sectors, as seen on the example. Example: http://en.wikipedia.org/wiki/Verkhovna_Rada#mediaviewer/File:Fractions_of_th e_Parliament_of_Ukraine.svg Found here (on the right, a bit down): http://en.wikipedia.org/wiki/Verkhovna_Rada Hi Stefan, 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. Jim __ R-help@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.
Re: [R] Parliament Seats Graph
On Fri, Sep 12, 2014 at 11:25 AM, Jim Lemon j...@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? Barry __ R-help@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.
Re: [R] Parliament Seats Graph
Yes. That's correct. The main problem is to solve a matrix where the colSums and rowSums are known. Credits to dwinsem...@comcast.net for pointing out the function r2dtable to me. Just feed it with the known margins and the number of matrices You want. And Bob is Your uncle! Look at the thread Margins to fill matrix that I started on the subject. 2014-09-12 13:18 GMT+02:00 Barry Rowlingson b.rowling...@lancaster.ac.uk: On Fri, Sep 12, 2014 at 11:25 AM, Jim Lemon j...@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? Barry __ R-help@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.
Re: [R] Parliament Seats Graph
On 12/09/2014, 7:18 AM, Barry Rowlingson wrote: On Fri, Sep 12, 2014 at 11:25 AM, Jim Lemon j...@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@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.
Re: [R] Parliament Seats Graph
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.dun...@gmail.com wrote: On 12/09/2014, 7:18 AM, Barry Rowlingson wrote: On Fri, Sep 12, 2014 at 11:25 AM, Jim Lemon j...@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@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.
Re: [R] Parliament Seats Graph
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.rowling...@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.dun...@gmail.com wrote: On 12/09/2014, 7:18 AM, Barry Rowlingson wrote: On Fri, Sep 12, 2014 at 11:25 AM, Jim Lemon j...@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@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. __ R-help@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.
[R] Parliament Seats Graph
Hi, Is there any package (or homegrown function) that can produce Parliament Seats Graph? I'm referring to the nice looking concentric half circles of colored seats as seen on Wikipedia (for example). I can pretty easily plot the points and color them. But I can't group the colored points in sectors, as seen on the example. Example: http://en.wikipedia.org/wiki/Verkhovna_Rada#mediaviewer/File:Fractions_of_the_Parliament_of_Ukraine.svg Found here (on the right, a bit down): http://en.wikipedia.org/wiki/Verkhovna_Rada TIA __ R-help@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.