Dim*_*rob 7 charts r waffle-chart
我正在寻找一种方法来做一个(实际上很常见的)可视化,通过一组N个象形图显示将N个单元的分组分成几个类别 - 我更喜欢填充的正方形; 在报纸等中,人们可能会看到很少的人形形状 - 每个象形图根据第n个单元的类别着色.N固定为1或100,显示分数或百分比而非计数的图表可以.彩色的"条纹"必须包裹多行.是否有人知道R包中有此图表?
举一个具体的例子,
x =样本(c("A","B"),100,替换= T)
我想看一个10x10(或5x20,或其他)网格的彩色正方形,一些 - 对应于"A" - 彩色绿色,其他红色.绿色(同样是红色)方块可以"聚集",或保留原始顺序.
谢谢.
下面我们展示3个版本.第一个使用正方形,下一个使用圆形,下一个使用一个人的图标,最后我们使用笑脸.
广场
# input data
set.seed(123)
x <- sample(c("A","B"),100,replace = T)
# input parameters - nr * nc should equal length(x)
cols <- c("green", "red")
nr <- 10
nc <- 10
# create data.frame of positions and colors
m <- matrix(cols[factor(x)], nr, nc)
DF <- data.frame(row = c(row(m)), col = c(col(m)[, nc:1]), value = c(m),
stringsAsFactors = FALSE)
# plot squares - modify cex to get different sized squares
plot(col ~ row, DF, col = DF$value, pch = 15, cex = 4, asp = 1,
xlim = c(0, nr), ylim = c(0, nc),
axes = FALSE, xlab = "", ylab = "")
Run Code Online (Sandbox Code Playgroud)

界
# plot circles
plot(col ~ row, DF, col = DF$value, pch = 20, cex = 6, asp = 1,
xlim = c(0, nr), ylim = c(0, nc),
axes = FALSE, xlab = "", ylab = "")
Run Code Online (Sandbox Code Playgroud)

png图标此解决方案使用我们假设的人的黑/白图标已保存在当前目录中man.png.我们将其颜色为红色和绿色,并使用这两个版本代替正方形或圆形:
# blank graph to insert man icons into
plot(col ~ row, DF, col = DF$value, asp = 1,
xlim = c(0, nr), ylim = c(0, nc),
axes = FALSE, xlab = "", ylab = "", type = "n")
library(png)
man <- readPNG("man.png")
red.man <- man
red.man[,,1] <- man[,,4] # fill in red dimension
R <- subset(DF, value == "red")
with(R, rasterImage(red.man,
row-.5, col-.5, row+.5, col+.5,
xlim = c(0, nr), ylim = c(0, nc),
xlab = "", ylab = ""))
green.man <- man
green.man[,,2] <- man[,,4] # fill in green dimension
G <- subset(DF, value == "green")
with(G, rasterImage(green.man,
row-.5, col-.5, row+.5, col+.5,
xlim = c(0, nr), ylim = c(0, nc),
xlab = "", ylab = ""))
Run Code Online (Sandbox Code Playgroud)

笑脸图标此解决方案使用绿色笑脸图标和红色皱眉图标我们假设已将其保存在当前目录中作为smiley_green.jpg和smiley_red.jpg.
# blank graph to insert man icons into
xp <- 1.25
plot(col ~ row, DF, col = DF$value, asp = 1,
xlim = c(0, xp * nr), ylim = c(0, xp * nc),
axes = FALSE, xlab = "", ylab = "", type = "n")
library(jpeg)
smiley_green <- readJPEG("smiley_green.jpg")
smiley_red <- readJPEG("smiley_red.jpg")
R <- subset(transform(DF, row = xp * row, col = xp * col), value == "red")
with(R, rasterImage(smiley_red,
row - .5, col - .5, row + .5, col + .5,
xlim = c(0, xp * nr), ylim = c(0, xp * nc),
xlab = "", ylab = ""))
G <- subset(transform(DF, row = xp * row, col = xp * col), value == "green")
with(G, rasterImage(smiley_green,
row - .5, col - .5, row + .5, col + .5,
xlim = c(0, xp * nr), ylim = c(0, xp * nc),
xlab = "", ylab = ""))
Run Code Online (Sandbox Code Playgroud)

修改为10x10绿色/红色并使用man图标添加版本.
答对了.我的祈祷得到了答复
http://rud.is/b/2015/03/18/making-waffle-charts-in-r-with-the-new-waffle-package/
(但非常感谢前面提到的解决方案).
# devtools::install_github("hrbrmstr/waffle")
library(waffle)
x <- sample(c("A","B"),100,replace = T)
x <- c(A=sum(x=="A"), B=sum(x=="B"))
waffle(x, rows=10, colors=c("red", "green"))
Run Code Online (Sandbox Code Playgroud)
