This page aims to describe how to realise an interactive circle packing chart. It has a major advantage: when hovering circles, additional information appears, what gives more insight to the plot.
This chart follows the pages #306 and #305 that explains how to realise a static version of circle packing, and how to customise it. This interactive version is very close to the static one. It uses the ggiraph library to transform the ggplot2 code in something interactive. The steps are quite easy. First you need to prepare a column in the data frame with the text you want to display while hovering. Second, you need to change the geometries to use the interactive geometries of ggiraph. Check the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# libraries library(packcircles) library(ggplot2) library(viridis) library(ggiraph) # Create data data=data.frame(group=paste("Group_", sample(letters, 70, replace=T), sample(letters, 70, replace=T), sample(letters, 70, replace=T), sep="" ), value=sample(seq(1,70),70)) # Add a column with the text you want to display for each bubble: data$text=paste("name: ",data$group, "\n", "value:", data$value, "\n", "You can add a story here!") # Generate the layout packing <- circleProgressiveLayout(data$value, sizetype='area') data = cbind(data, packing) dat.gg <- circleLayoutVertices(packing, npoints=50) # Make the plot with a few differences compared to the static version: p=ggplot() + geom_polygon_interactive(data = dat.gg, aes(x, y, group = id, fill=id, tooltip = data$text[id], data_id = id), colour = "black", alpha = 0.6) + scale_fill_viridis() + geom_text(data = data, aes(x, y, label = gsub("Group_", "", group)), size=2, color="black") + theme_void() + theme(legend.position="none", plot.margin=unit(c(0,0,0,0),"cm") ) + coord_equal() widg=ggiraph(ggobj = p, width_svg = 7, height_svg = 7) |
Related
Make a search
Leave a Reply