我正在寻找一种进行可视化的方法(实际上非常普遍),即通过一组N个象形图将N个单位的人口分成几个类别的显示-我更喜欢填充的正方形;在报纸等中,可能会看到很少的类人动物形状-每个象形图根据第n个单位的类别进行着色。 N固定为1或100,并且显示分数或百分比而不是计数的图表就可以了。彩色的“条纹”将不得不包装成多行。有人知道R包中提供此图表吗?
举一个具体的例子
x =样本(c(“A”,“B”),100,replace = 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 = "")
圈子
# 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图标此解决方案使用黑色/白色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 = ""))
笑脸图标此解决方案使用了green smiley face icon和red frowning face icon(我们假设它们已保存在
当前目录为
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 = ""))
修订版为10x10绿色/红色,并使用man图标添加了版本。
关于r - 想要: repeated-pictogram visualization of population split,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27401806/