Wanted: repeated-pictogram visualization of population split

核能气质少年 提交于 2019-11-30 09:29:04

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.

Dimitri Shvorob

BINGO. My prayers have been answered with

http://rud.is/b/2015/03/18/making-waffle-charts-in-r-with-the-new-waffle-package/

(But many thanks for the solutions suggested earlier).

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

I also made a package for this (nigh-simultaneously with that other guy :-). It has three approaches, using geom_tile, geom_text (use e.g. FontAwesome for icons) and geom_point. The latter two can do drop shadows, but you have to tinker with the settings sometimes. There's some 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")

The OP requested a ggplot2 solution on the ggplot2 Google group, so I'm posting it here as well:

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!