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

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

问题描述

我正在寻找一种方法来进行(实际上很常见)可视化,即通过一组 N 个象形图将 N 个单位的总体划分为几个类别 - 我更喜欢实心方块;在报纸等中,人们可能会看到很小的人形——每个象形图根据第 n 个单元的类别着色.N 固定为 1 或 100,并且图表显示分数或百分比而不是计数,就可以了.彩色条纹"必须包装成多行.有人知道 R 包中提供的此图表吗?

举一个具体的例子,

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

我想看到一个 10x10(或 5x20,或其他)的彩色方块网格,一些 - 对应于A" - 绿色,其他红色.绿色(和红色)方块可以成束",或者保持原来的顺序.

谢谢.

解决方案

下面我们展示了 3 个版本.第一个使用正方形,接下来使用圆形,接下来使用一个男人的图标,最后我们使用笑脸.

正方形

# 输入数据set.seed(123)x <- sample(c("A","B"),100,replace = T)# 输入参数 - nr * nc 应该等于 length(x)cols <- c("green", "red")nr <- 10nc <- 10# 创建位置和颜色的 data.framem <- 矩阵(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 - 修改 cex 以获得不同大小的方块绘图(列〜行,DF,列 = DF$ 值,pch = 15,cex = 4,asp = 1,xlim = c(0, nr), ylim = c(0, nc),轴 = FALSE,xlab = "",ylab = "")

圆圈

# 绘制圆圈绘图(列〜行,DF,列 = DF$ 值,pch = 20,cex = 6,asp = 1,xlim = c(0, nr), ylim = c(0, nc),轴 = FALSE,xlab = "",ylab = "")

png 图标 此解决方案使用黑色/白色 和我们假设有的 红色皱眉脸图标被保存在当前目录为 smiley_green.jpgsmiley_red.jpg.

# 空白图形插入男人图标xp <- 1.25绘图(列〜行,DF,列 = DF$ 值,asp = 1,xlim = c(0, xp * nr), ylim = c(0, xp * nc),轴 = FALSE, xlab = "", ylab = "", type = "n")图书馆(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,行 - .5,列 - .5,行 + .5,列 + .5,xlim = c(0, xp * nr), ylim = c(0, xp * nc),xlab = "", ylab = ""))G <- 子集(变换(DF,行 = xp * 行,col = xp * col),值 == 绿色")with(G, rasterImage(smiley_green,行 - .5,列 - .5,行 + .5,列 + .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天全站免登陆