preface
Waffle charts are divided into block waffles and dot waffles. It is a valid chart showing the group category of total data. It is a western bread made of small squares, hence the name waffle chart.
In this paper, the framework
Data is introduced
Library (RColorBrewer) Library (reshape2)Copy the code
nrows <- 10
categ_table <- round(table(mpg$class ) * ((nrows*nrows)/(length(mpg$class))))
sort_table<-sort(categ_table,index.return=TRUE,decreasing = FALSE)
Order<-sort(as.data.frame(categ_table)$Freq,index.return=TRUE,decreasing = FALSE)
df <- expand.grid(y = 1:nrows, x = 1:nrows)
df$category<-factor(rep(names(sort_table),sort_table), levels=names(sort_table))
Color<-brewer.pal(length(sort_table), "Set2")
head(df)
Copy the code
The first six rows are as follows, y goes from 1 to 10, and x goes from 1 to 10.
Ggplot package drawing
Block waffle chart
The small squares of the block waffle chart, with different colors representing different categories, are suitable for quickly viewing the distribution and proportions of different categories in a data set and comparing them with those in other data sets, making it easier to identify patterns.
The next step is to implement it through the above data. The code looks long, but half of it is the theme() setup.
ggplot(df, aes(x = y, y = x, fill = category)) + geom_tile(color = "white", Size = 0.25) + #geom_point(color = "black",shape=1,size=5) + coord_fixed(ratio =1)+ #x, Scale_x_continuous (trans = 'expand ') +#expand = c(0, 0) scale_y_continuous(trans = 'reverse') +#expand = c(0, 0), scale_fill_manual(name = "Category", #labels = names(sort_table), values = Color)+ theme(#panel.border = element_rect(fill=NA,size = 2), Panel. Background = element_blank(), plot.title = element_text(size = rel(1.2)), axis. axis.title = element_blank(), axis.ticks = element_blank(), legend.title = element_blank(), legend.position = "right")Copy the code
Dot waffle chart
Dot waffles figure (dot matrix chart) units shows that the discrete data points, each color points representing a specific category, and in the form of matrix together, suitable for quick review data concentration distribution and the proportion of different categories, and comparing with other data set of distribution and proportion, making it easier to find out of mode. When there is only one variable/category (all the dots are the same color), the dot waffle chart corresponds to the scale area chart
Ggplot (df, AES (x0 = y, y0 = x, fill = category,r=0.5)) + geom_circle(color = "black", Size = 0.25) + #geom_point(color = "black",shape=21,size=6) + coord_fixed(ratio = 1)+ scale_x_continuous(trans = 'reverse') +#expand = c(0, 0), scale_y_continuous(trans = 'reverse') +#expand = c(0, 0), scale_fill_manual(name = "Category", #labels = names(sort_table), values = Color)+ theme(#panel.border = element_rect(fill=NA,size = 2), panel.background = element_blank(), Plot.title = element_text(size = rel(1.2)), legend. Position = "right")Copy the code
Illustration: When y is 10, there are two PichUps, one subcompact, and so on. If you look at x, at x equals 10, they’re all SUVs. Overall, SUVs accounted for the most (16) and 2seaters the least (two).
Stacked waffle chart
Here’s another interesting waffle chart.
library(dplyr)
nrows <- 10
ndeep <- 10
unit<-100
df <- expand.grid(y = 1:nrows, x = 1:nrows)
categ_table <- as.data.frame(table(mpg$class) * (nrows*nrows))
colnames(categ_table)<-c("names","vals")
categ_table<-arrange(categ_table,desc(vals))
categ_table$vals<-categ_table$vals /unit
tb4waffles <- expand.grid(y = 1:ndeep,x = seq_len(ceiling(sum(categ_table$vals) / ndeep)))
regionvec <- as.character(rep(categ_table$names, categ_table$vals))
tb4waffles<-tb4waffles[1:length(regionvec),]
tb4waffles$names <- factor(regionvec,levels=categ_table$names)
Color<-brewer.pal(nrow(categ_table), "Set2")
Copy the code
ggplot(tb4waffles, aes(x = x, y = y, fill = names)) + #geom_tile(color = "white") + # geom_point(color = "black",shape=21,size=5) + # scale_fill_manual(name = "Category", values = Color)+ xlab("1 square = 100")+ ylab("")+ coord_fixed(ratio = 1)+ theme(#panel.border = Element_rect (fill=NA,size = 2), panel. Background = element__blank (), plot.title = element_text(size = rel(1.2)), #axis.text = element_blank(), #axis.title = element_blank(), #axis.ticks = element_blank(), # legend.title = element_blank(), legend.position = "right")Copy the code
Waffle pack drawing
Of course, if the previous code is too hard to read, here’s a handy package for waffle charts.
waffle(parts, rows = 10, keep = TRUE, xlab = NULL, title = NULL, colors = NA, size = 2, flip = FALSE, reverse = FALSE, equal = TRUE, pad = 0, use_glyph = FALSE, glyph_size = 12, legend_pos = "right")
Copy the code
Main parameter Meanings:
- Parts is a named vector for the values of the diagram
- The number of rows in the rows block
- Keep keeps factor levels (for example, get a consistent legend in a waffle chart)
A simple example
In this waffle chart, behavior 8, 80 are one, 30 are two, 20 are tree, and 10 are four.
parts <- c(One=80, Two=30, Three=20, Four=10)
chart <- waffle(parts, rows=8)
print(chart)
Copy the code
This film mainly refers to the partial integral chart in chapter 7 of the Beauty of R Data Visualization. The supporting code can be seen in Github and Waffle package introduction of Zhang Jie