Blockquote
I'm a novice both at R and at formatting in Stack Overflow, so please bear with me. I'm trying to create a sunburst pie chart and a regular pie chart for my company but I've hit a wall with Excel. I'd like to replicate the charts in the attached images in R but have the flexibility to modify things. For example, the ability to move a label for a small slice of the pie outside of the pie chart and have a line that points to the slice. Or wrapping the label inside a large slice of the pie such that the curve of the label matches the curve of the circle.
I'd also appreciate any help in making the sunburst plot look more professional or presentable. I'm attaching an image of the two plots in Excel and an image of the data since I don't know how to add tables here. If you post your code, I can try to understand how it works by "breaking" it. This was very helpful in making and modifying another complicated plot when I asked for help a couple years ago.
CodePudding user response:
Producing a bespoke plot like this in R isn't trivial. Firstly, you need to have your data in a format the lends itself to plotting:
df <- data.frame(Allocation = c(rep("Activation", 8), "Swap", "Unissued"),
Block = c(rep("Developer", 5), rep("Grant", 3), NA, NA),
Work = c("Company A / Company B", "Pre-company Work",
"Advisory", "Protocol Contributors",
"Developers / incentives", "General Support",
"Projects", "Market makers and advisory", NA, NA),
Percent = c(15.8, 0.9, 0.3, 0.7, 0.3, 2.4, 2.4, 7.2, 10, 60))
df
#> Allocation Block Work Percent
#> 1 Activation Developer Company A / Company B 15.8
#> 2 Activation Developer Pre-company Work 0.9
#> 3 Activation Developer Advisory 0.3
#> 4 Activation Developer Protocol Contributors 0.7
#> 5 Activation Developer Developers / incentives 0.3
#> 6 Activation Grant General Support 2.4
#> 7 Activation Grant Projects 2.4
#> 8 Activation Grant Market makers and advisory 7.2
#> 9 Swap <NA> <NA> 10.0
#> 10 Unissued <NA> <NA> 60.0
The left-hand plot can be created using ggplot
with polar co-ordinates, but you need to summarize your data first to add up all the percentages for each Allocation
group. You can make the labels curved with geomtextpath
:
library(tidyverse)
library(geomtextpath)
df %>%
group_by(Allocation) %>%
summarise(Percent = sum(Percent)) %>%
ggplot(aes(x = 1, y = Percent, fill = Allocation))
geom_col(color = "white")
geom_textpath(aes(label = Allocation), colour = "white", spacing = 100,
angle = 90, size = 5, fontface = 2,
position = position_stack(vjust = 0.5))
coord_polar(theta = "y", direction = -1)
scale_x_continuous(limits = c(-0.5, 2))
scale_fill_manual(values = c("#4285f4", "#ea4335", "#fbbc04"))
theme_void()
theme(legend.position = "none")
You can change the colours inside scale_fill_manual
to suit your preference.
The right hand plot is even more involved, but hopefully this is enough to get you started.
CodePudding user response:
So I played around with this a little more and found an example using a Plotly package that I installed in R. I managed to make an interactive Pie chart with multiple levels. If I click on the Developer Block or Grant Block then the entire chart fills with blue or orange (I'll work on curved text later). .
Here's the code:
library(plotly)
fig <- plot_ly(
labels = c("Grant Block", "Developer Block", "Comp A / Comp B", "Pre-Comp Work", "Advisory", "Contributors", "Devs / Incentives", "General Support", "Projects", "MM and Advisory"),
parents = c("", "", "Developer Block", "Developer Block", "Developer Block", "Developer Block", "Developer Block", "Grant Block", "Grant Block", "Grant Block"),
values = c(40, 60, 52.98, 2.58, 1.14, 2.22, 1.08, 8, 8, 24),
type = 'sunburst',
branchvalues = 'total'
)
fig
This generates an interactive plot in HTML in Microsoft Edge. My next question is probably trivial since I'm a novice, but what do I do with this now? I tried to save as HTML but it's no longer interactive. How can I save the interactive plot to my computer and more importantly, embed this into a website?