R 用选举结果划分地图?
我想用2013年的选举结果垂直划分一张德国地图:R 用选举结果划分地图?,r,R,我想用2013年的选举结果垂直划分一张德国地图: > VoteGermany2013 Party Result 1 CDU/CSU 49.4 2 SPD 30.5 3 LINKE 10.2 4 GRUENE 10.0 我想要的是非常简单的:一个像条形图一样分割的地图,CDU/CSU的长度占49.4%,SPD的长度占30.5%,依此类推。这就像是一个被分割成几个部分的酒吧,但其形式是一个国家 对于地图,我使用: library(maps) librar
> VoteGermany2013
Party Result
1 CDU/CSU 49.4
2 SPD 30.5
3 LINKE 10.2
4 GRUENE 10.0
我想要的是非常简单的:一个像条形图一样分割的地图,CDU/CSU的长度占49.4%,SPD的长度占30.5%,依此类推。这就像是一个被分割成几个部分的酒吧,但其形式是一个国家
对于地图,我使用:
library(maps)
library(mapdata)
map("worldHires","Germany")
使用
rect
创建条形图,覆盖国家边界,并修剪边界周围的矩形
library('maps')
library('mapdata')
## fill = TRUE !important
dat <- map("worldHires", "Germany", fill = TRUE)
## set up plotting window
p <- par('usr')
plot.new()
plot.window(p[1:2], p[3:4])
## make some data and calculate some useful things
pcts <- prop.table(table(sample(1:3, 20, replace = TRUE)))
xx <- range(dat$x, na.rm = TRUE)
yy <- range(dat$y, na.rm = TRUE)
zz <- diff(yy) * pcts
cols <- palette(rainbow(length(zz)))
cols <- c('red','black','gold')
## draw and color rectangles, you can do this many other ways
dy <- 0
for (ii in seq_along(zz)) {
rect(p[1], yy[1] + dy,
p[2], yy[1] + (dy <- sum(zz[1:ii])),
col = cols[ii], border = NA)
}
map('worldHires', 'Germany', col = 'black', add = TRUE)
## trim around borders
xb <- xx + c(-1, 1)
yb <- yy + c(-1, 1)
polypath(c(dat$x, NA, c(xb, rev(xb))), c(dat$y, NA, rep(yb, each = 2)),
col = 'white', rule = 'evenodd')
功能:
evelyne <- function(x, region, labels, cols) {
op <- par(no.readonly = TRUE)
on.exit(par(op))
require('maps')
require('mapdata')
## fill = TRUE !important
dat <- map("worldHires", region, fill = TRUE)
## set up plotting window
p <- par('usr')
plot.new()
plot.window(p[1:2], p[3:4])
## calculate some useful things
xx <- range(dat$x, na.rm = TRUE)
yy <- range(dat$y, na.rm = TRUE)
zz <- diff(yy) * x
if (missing(cols))
cols <- palette(rainbow(length(x)))
## draw and color rectangles
dyy <- rep(0, length(zz))
dy <- 0
for (ii in seq_along(zz)) {
rect(p[1], yy[1] + dy, p[2], yy[1] + (dy <- sum(zz[1:ii])),
col = cols[ii], border = NA)
## label y-coordinates
dyy[ii] <- yy[1] + c(0, cumsum(zz))[ii] + zz[ii] / 2
}
map('worldHires', region, col = 'black', add = TRUE)
## trim around borders
xb <- xx + c(-1,1)
yb <- yy + c(-1,1)
polypath(c(dat$x, NA, c(xb, rev(xb))), c(dat$y, NA, rep(yb, each = 2)),
col = 'white', rule = 'evenodd')
if (!missing(labels))
text(max(xx), dyy, labels = labels, pos = 4, xpd = NA)
}
非常感谢您的帮助,这真的很有帮助。由于我对R非常陌生,我不确定我可以在哪里输入数据,但是…@Evelyne1991只需将
pcts
替换为您的:pcts@Evelyne1991添加了一个函数,使其(希望)更易于使用,如果您想评论这是一个非常糟糕的数据可视化,还可以标记这些部分,因为彩色区域歪曲了基本数字。垂直分割多边形可以使面积成比例,但这比线性分割要复杂一些。@rawr这太神奇了。再次感谢。
evelyne <- function(x, region, labels, cols) {
op <- par(no.readonly = TRUE)
on.exit(par(op))
require('maps')
require('mapdata')
## fill = TRUE !important
dat <- map("worldHires", region, fill = TRUE)
## set up plotting window
p <- par('usr')
plot.new()
plot.window(p[1:2], p[3:4])
## calculate some useful things
xx <- range(dat$x, na.rm = TRUE)
yy <- range(dat$y, na.rm = TRUE)
zz <- diff(yy) * x
if (missing(cols))
cols <- palette(rainbow(length(x)))
## draw and color rectangles
dyy <- rep(0, length(zz))
dy <- 0
for (ii in seq_along(zz)) {
rect(p[1], yy[1] + dy, p[2], yy[1] + (dy <- sum(zz[1:ii])),
col = cols[ii], border = NA)
## label y-coordinates
dyy[ii] <- yy[1] + c(0, cumsum(zz))[ii] + zz[ii] / 2
}
map('worldHires', region, col = 'black', add = TRUE)
## trim around borders
xb <- xx + c(-1,1)
yb <- yy + c(-1,1)
polypath(c(dat$x, NA, c(xb, rev(xb))), c(dat$y, NA, rep(yb, each = 2)),
col = 'white', rule = 'evenodd')
if (!missing(labels))
text(max(xx), dyy, labels = labels, pos = 4, xpd = NA)
}
evelyne(pcts, 'Germany', cols = c('red','black','gold'))
with(voteGermany2013,
evelyne(Result / 100, 'Germany', labels = sprintf('%s (%s%%)', Party, Result)))