Parliamentary Schedule & # 8594; colors and labels?

I use the following code to create a schedule of parliamentary seats with R for the 2013 election results in Germany.

I would like to change the colors for each side (CDU / CSU β†’ red, SPD β†’ blue, Linke β†’ yellow and Gruene β†’ green). When I try to do this, the colors seem random, destroying the sequence of sides in the graph.

I also want to remove the black outline of the graph in order to display only the graph of places.

VoteGermany2013 <- data.frame(Party=c( "CDU/CSU", "SPD", "LINKE","GRUENE"), Result=c(311,193,64,63)) 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 } election <- function(seats, counts){ stopifnot(sum(counts)==nrow(seats)) seats$party = rep(1:length(counts),counts) seats } layout = seats(631,16) result = election(layout, VoteGermany2013$Result) # no overall majority!!! plot(result$x, result$y, col=result$party,pch=19, asp=1) 

My current graph

+6
source share
1 answer

Good example. I suppose you want to crush the axes. This uses the numerical values ​​of result$party as an index in the color vector you specify. The vector col (which is created by the index at equal lengths with the arguments x and y) should be as long as the values ​​are "x" and "y", unless there is regularity that synchronizes with the length of the column. (If the colors repeat their grouping synchronously with a multiple of the length of the col-vector, then there is no problem. Disposal takes care of everything.) Without regularity in the grouping, the col-vector is recycled, and chaos.

 plot(result$x, result$y, col=c( "red", "blue", "yellow","green")[result$party], #numeric index pch=19, asp=1, frame.plot=FALSE, # gets rid of the surrounding rectangle axes="F") # gets rid of the numbers and ticks 

You can suppress "xlab" and "ylab" by assigning ""

enter image description here

+8
source

Source: https://habr.com/ru/post/983474/


All Articles