Skip to content

Instantly share code, notes, and snippets.

@mtreg
Created August 7, 2019 00:36
Show Gist options
  • Save mtreg/736f1c454c87ca5cf1b7020756cb722c to your computer and use it in GitHub Desktop.
Save mtreg/736f1c454c87ca5cf1b7020756cb722c to your computer and use it in GitHub Desktop.
Gist for post about how to go from regular dataframe in R to format needed for plotly sunburst
library(plotly)
# Create some fake data - say ownership and land use data with acreage
df <- data.frame(ownership=c(rep("private", 3), rep("public",3),rep("mixed", 3)),
landuse=c(rep(c("residential", "recreation", "commercial"),3)),
acres=c(108,143,102, 300,320,500, 37,58,90))
# Just try some quick pie charts of acreage by landuse and ownership
plot_ly(data=df, labels= ~landuse, values= ~acres, type='pie')
plot_ly(data=df, labels= ~ownership, values= ~acres, type='pie')
# This doesn't render anything... not that I'd expect it to given the data format doesn't seem to match what's needed,
# but this is what I'd intuitively expect to work..
plot_ly(data=df, labels= ~landuse, parents = ~ownership, values= ~acres, type='sunburst')
# Is there an easy way to go from the dataframe, df, to hierarchical format needed for sunburst?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment