Requires: repeated pictogram of visualization of the division of the population

I’m looking for a way to make a (actually quite ordinary) visualization, as a result of which the splitting of a population, say, N units into several categories, is shown using a set of N pictograms - I would prefer filled squares; in a newspaper, etc. you can see small humanoid figures - each icon will be colored in accordance with the category of the nth unit. N set to 1 or 100, and a chart showing shares or percentages, not bills, will be fine. Colored "stripes" would have to be wrapped over several lines. Does anyone know this diagram available in package R?

To take a concrete example,

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

I would like to see a grid of 10x10 (or 5x20 or any other) of colored squares, some - the corresponding "A" - colored green, others - red. Green (and similar red) squares can be β€œgrouped” or left in their original order.

Thanks.

+6
source share
4 answers

Below we show 3 versions. The first uses squares, and the next uses circles, the next uses a human icon, and finally, we use emoticons.

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

enter image description here

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

enter image description here

png Icons This solution uses the black and white icon of the person we assumed was saved in the current man.png directory. We color it red and green and use these two versions instead of 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 = "")) 

enter image description here

Emoticon Icons This solution uses the green emoticon icon and the red frown face icon , which we assumed were 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 = "")) 

enter image description here

Revised to 10x10 green / red and an added version using the person icon.

+7
source

BINGO. Answered my prayers

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

(But many thanks for the previously proposed solutions).

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

enter image description here

+5
source

The OP requested the ggplot2 solution in the ggplot2 group of Google, so I also posted it here:

 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) 

enter image description here

+2
source

I also made a package for this (almost simultaneously with this other guy :-). It has three approaches using geom_tile, geom_text (use, for example, FontAwesome for icons) and geom_point. The last two can make shadows, but sometimes you have to change the settings. There are 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") 

simple counts

counts of travelers

+2
source

Source: https://habr.com/ru/post/979402/


All Articles