Last active
August 29, 2015 14:17
-
-
Save jalapic/791d63669cabdd36a79f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 ! |
as_rcdimple
can't handle the R colors. Will try to demo a conversion.
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")
}
' )))
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
The title is very robust. It will always be rooted to the top left of the plot. The footer/
xlab
is more tricky - it will move around a bit depending upon the number of blocks on the bottom. I guess it would be possible to work out the ideal value ofhjust
based upon the number of columns and size of each square in the plot. I did briefly look into other options, e.g.grid.text
,geom_annotate
but all of these are worse in terms of fixing the text at the bottom to the bottom right corner of the plot. I think the answer given to a question by @baptiste here - http://stackoverflow.com/questions/26389154/538-ify-ggplot-output-justifying-picturegrob-in-r-grid-graphics may have some merit for this issue, but seems too complicated for this purpose. Hope this is useful. Thanks for a great package.,