通缉:重复象形图可视化的人口分裂 [英] Wanted: repeated-pictogram visualization of population split

查看:180
本文介绍了通缉:重复象形图可视化的人口分裂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来做一个(实际上很常见的)可视化,通过一组N象形图显示一个人口,例如N个单元分成几个类别 - 我喜欢填充的方块;在报纸等中,可以看到小的人形形状 - 每个象形图根据第n个单位的类别着色。 N固定为1或100,图表显示分数或百分比而不是计数,将是OK。彩色的条纹必须换行到多行。



如需具体示例,



x =示例(c(A,B),100,replace = T)



我想看到一个10x10(或5x20,彩色正方形,一些 - 对应于A - 彩色绿色,其他红色。绿色(同样是红色)正方形可以是束,或以原始顺序留下。



谢谢。

解决方案

下面我们显示3个版本。第一个使用方块,下一个使用圆圈,接下来使用一个男人的图标,最后我们使用笑脸。



方块 p>

 #input data 
set.seed(123)
x
#输入参数 - nr * nc应该等于length(x)
cols < - c(green,red)
nr < - 10
nc < - 10

#创建位置和颜色的数据框架
m DF < - data.frame(row = c(row(m)),col = c(col(m)[,nc:1] ,
stringsAsFactors = FALSE)

#plot squares - 修改cex以获得不同大小的正方形
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 = $ b



圈子

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


b $ b

>



png图标此解决方案使用黑色/白色和红色皱眉脸图标,我们假定已将其保存在
当前目录中为 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)

库(jpeg)
smiley_green< - readJPEG(smiley_green.jpg)
smiley_red< - readJPEG(smiley_red.jpg)

R <子集使用(R,rasterImage(smiley_red,
row - .5,col - .5),transform(DF,row = xp * row,col = xp * col),value ==red row + .5,col + .5,
xlim = c(0,xp * nr),ylim = c(0,xp * nc),
xlab =,ylab = )

G <子集(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 =))



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


I am looking for a way to do a (actually quite common) visualization, whereby the split of a population of, say, N units into several categories is shown via a set of N pictograms - I'd prefer filled squares; in a newspaper, etc., one might see little humanoid shapes - with each pictogram colored according to the category of n'th unit. N fixed to 1 or 100, and the chart displaying fractions or percentages rather than counts, would be OK. The colored "stripes" would have to wrap to multiple lines. Is anyone aware of this chart available in an R package?

To take a specific example,

x = sample(c("A","B"),100,replace = T)

I would like to see a 10x10 (or 5x20, or whatever) grid of colored squares, some - corresponding to "A" - colored green, others red. The green (and likewise red) squares could be "bunched", or left in the original order.

Thank you.

解决方案

Below we show 3 versions. The first uses squares, the next uses circles, the next uses an icon of a man and finally we use smiley faces.

Squares

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

Circles

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

png Icons This solution uses a black/white icon of a man which we have assumed has been saved in the current directory as man.png. We color it red and green and use those two versions in place of the squares or circles:

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

Smiley Face Icons This solution uses a green smiley face icon and a red frowning face icon which we have assumed have been saved in the current directory as smiley_green.jpg and 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 = ""))

Revised To 10x10 green/red and added version using man icon.

这篇关于通缉:重复象形图可视化的人口分裂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆