I am trying to create a waffle plot. My data and waffle plot looked like this-
library(waffle)
parts=c("Extraction"=397, "Operculectomy"=11, "Medication"=3)
waffle(parts, row=12, colors=c("#CC0000", "#006600", "#669999"))
I want to add some text on the label of the legend section of this plot like below:
Extraction (397/413)
Operculectomy (11/413)
Medication (3/413)
Based on the documentation of the waffle
package, I'm not sure you can set a lot of parameters to your waffle
plots.
However, you can modify category names of your data before by doing:
library(waffle)
parts=c("Extraction"=397, "Operculectomy"=11, "Medication"=3)
names(parts) = paste0(names(parts),"(",parts,"/",sum(parts),")")
waffle(parts, row=12, colors=c("#CC0000", "#006600", "#669999"))
Which gives you the following graph:
Alternative using ggplot2
Alternatively you can do a "waffle" plot by using geom_tile
from ggplot2
. The procedure is a little bit less straightforward but at least you can enjoy the full customization tools of ggplot2
.
To get the same graph, you can do:
library(ggplot2)
categ_table = c("Extraction"=397, "Operculectomy"=11, "Medication"=3)
df <- expand.grid(y = 1:10, x = 1:(round(sum(categ_table)/10)+1))
df$Category = factor(c(rep(names(categ_table),categ_table),rep(NA,nrow(df)-sum(categ_table))))
df = df[!is.na(df$Category),]
ggplot(df, aes(x = x, y = y, fill = Category)) +
geom_tile(color = "white", size = 0.5) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))+
theme(legend.title = element_blank(),
panel.background = element_rect(fill = 'white', colour = 'white'),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank())+
scale_fill_manual(values = c("#CC0000", "#006600", "#669999"),
breaks = names(categ_table),
labels = c("Extraction (397/411)","Operculectomy (11/411)", "Medication (3/411)"))
And you get the following graph:
Hope it answers your question