Skip to content

Instantly share code, notes, and snippets.

@jalapic
Last active August 29, 2015 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalapic/791d63669cabdd36a79f to your computer and use it in GitHub Desktop.
Save jalapic/791d63669cabdd36a79f to your computer and use it in GitHub Desktop.
### Waffle Charts
#install waffle & rcdimple
devtools::install_github("timelyportfolio/rcdimple") # only for htmlwidget functionality
devtools::install_github("hrbrmstr/waffle")
library(waffle)
#data
ids <- c(1520, 656, 182, 35, 187, 193, 21, 56, 61, 26, 28)
#colors
mycolors <- c("orange4", "orange2", "orange", "yellow", "yellow3", "greenyellow", "darkolivegreen2", "darkolivegreen4", "darkseagreen3",
"darkturquoise", "dodgerblue", "blue4")
#generate initial plot
p <- waffle(ids/10, rows=8, colors=rev(mycolors))
# add title and footer
p +
ggtitle("Number of interactions initiated by dominance rank") +
xlab("Each block represents 10 initiated interactions") +
theme(
plot.title = element_text(hjust=0,vjust=1, size=rel(1.6)),
axis.title.x = element_text(face="italic", hjust=0.95,vjust=1, size=rel(1)))
# notes - see comments
### Second Example.
parts <- c(`Un-breached\nUS Population`=(318-11-79), `Premera`=11, `Anthem`=79)
p2 <- waffle(parts/10, rows=3, colors=c("#969696", "#1879bf", "#009bda"))
p2 +
ggtitle("Health records breaches as fraction of US Population") +
xlab("(One square == 10m ppl)") +
theme(
plot.title = element_text(hjust=0,vjust=1, size=rel(1.6)),
axis.title.x = element_text(face="italic", hjust=0.95,vjust=1, size=rel(1)))
EDIT:
#from example 1..
### as_rcdimple test
p1 <- p +
ggtitle("Number of interactions initiated by dominance rank") +
xlab("Each block represents 10 initiated interactions") +
theme(
plot.title = element_text(hjust=0,vjust=1, size=rel(1.6)),
axis.title.x = element_text(face="italic", hjust=0.95,vjust=1, size=rel(1)))
as_rcdimple(p1) # something funky going on !
@timelyportfolio
Copy link

This ain't pretty, but gets close.

library(waffle)
library(pipeR)

mycolors <- unname(unlist(Map(
  function(color){colorspace::hex(colorspace::RGB(t(col2rgb(color)/255)))},
  c("orange4", "orange2", "orange", "yellow", "yellow3", "greenyellow", "darkolivegreen2", "darkolivegreen4",  "darkseagreen3",   
      "darkturquoise", "dodgerblue", "blue4")
)))

#generate initial plot
p <- waffle(ids/10, rows=8, colors=rev(mycolors)) 

as_rcdimple(p, height = 270, width = 800) %>>%
  set_bounds( x = "5%", y = "5%", width = "75%", height =  "80%" ) %>>%
  add_legend( x = "80%", width = "10%", y = "10%", height = "90%" ) %>>%
  add_title( "Number of interactions initiated by dominance rank" ) %>>%
  tack( options = list(tasks = htmlwidgets::JS('
    function(){
      this.widgetDimple[0].axes.forEach(function(ax){
              ax.shapes.remove()
      })
      this.widgetDimple[0].svg.append("text")
        .attr("x", 500)
        .attr("y", 220)
        .style("font-style", "italic")
        .style("font-size", "60%")
        .text("Each block represents 10 initiated interactions")
    }
  ' )))

@jalapic
Copy link
Author

jalapic commented Mar 23, 2015

That's great - I will play around more with it. Interested to know if it's possible to further customize the hover text?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment