通缉:人口分裂的重复象形图可视化

时间:2014-12-10 13:06:51

标签: r charts waffle-chart

我正在寻找一种方法来做一个(实际上相当常见的)可视化,其中通过一组N个象形图显示将N个单元的分组分成几个类别 - 我更喜欢实心方块;在报纸等中,人们可能会看到很少的人形形状 - 每个象形图根据第n个单元的类别着色。 N固定为1或100,显示分数或百分比而非计数的图表可以。彩色的“条纹”必须包裹多行。是否有人知道R包中有此图表?

举一个具体的例子,

x =样本(c(“A”,“B”),100,replace = T)

我希望看到10x10(或5x20或其他)网格的彩色方块,有些 - 对应于“A” - 绿色,其他为红色。绿色(同样是红色)方块可以“聚集”,或保留原始顺序。

谢谢。

4 个答案:

答案 0 :(得分:8)

下面我们展示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 = "")

enter image description here

<强>圈子

# 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 = "")

enter image description here

png图标此解决方案使用我们假设的黑/白icon of a man已保存在当前目录中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 = ""))

enter image description here

笑脸图标此解决方案使用green smiley face iconred frowning face icon我们假设已将其保存在 当前目录为smiley_green.jpgsmiley_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 = ""))

enter image description here

修订至10x10绿色/红色并使用man图标添加版本。

答案 1 :(得分:5)

宾果。我的祈祷已经回答了

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"))

enter image description here

答案 2 :(得分:3)

我也为此制作了一个包(与其他人同时: - )。它有三种方法,使用geom_tile,geom_text(例如使用FontAwesome作为图标)和geom_point。后两者可以做阴影,但你有时必须修改设置。有一些more examples here

devtools::install_github("rubenarslan/formr")
library(formr)
qplot_waffle(rep(1:3,each=40,length.out=90))
library(ggplot2)
qplot_waffle_text(rep(1, each = 30), symbol = "", rows = 3) + ggtitle("Number of travellers in 2008")

simple counts

counts of travellers

答案 3 :(得分:2)

OP在ggplot2 Google群组上请求ggplot2解决方案,因此我也在此处发布:

ggplot(DF, aes(row, col, fill=value)) + 
  geom_tile(colour="white", lwd=2) + 
  scale_fill_manual(values=c("green","red")) +
  theme(panel.background=element_blank(),
        axis.text=element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank()) +
  guides(fill=FALSE)

enter image description here