gpt4 book ai didi

r - 通缉 : repeated-pictogram visualization of population split

转载 作者:行者123 更新时间:2023-12-04 09:47:35 24 4
gpt4 key购买 nike

我正在寻找一种方法来进行(实际上很常见)可视化,即通过一组 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 = "")

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 icon和一个 red 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 图标添加版本。

关于r - 通缉 : repeated-pictogram visualization of population split,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27401806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com