The following code works fine and produces the required graph as given below:
library(tidyverse)
library(ggiraphExtra)
library(moonBook)
ggPieDonut(data = acs, mapping = aes(pies = Dx, donuts = smoking), interactive = TRUE)
Wondering how to construct Pie Donut chart with facet functionality. My attempt is below:
ggPieDonut(data = acs, mapping = aes(pies = Dx, donuts = smoking), interactive = TRUE) +
facet_wrap(facets = vars(sex))
NULL
The problem
The code in your attempt doesn't work because when
interactive = TRUE
,ggPieDonut()
doesn't return a ggplot, but a htmlwidget:And
facet_wrap()
only works with ggplots.If you change to
interactive = FALSE
you get another problem:The geoms doesn't contain both values of
sex
, sofacet_wrap()
doesn't know how to facet on it.Possible workaround
A solution is to create two plots on different subsets of the data, and use
patchwork
to combine the two plots:Output:
Update 1 - as a function
As @MikkoMarttila suggested, it might be better to create this as a function. If I were to reuse the function, I would probably write it like this:
This can then be used to facet on however many categories we want, and on any dataset, for example:
Again, as suggested by @MikkoMarttila, this can be piped into
ggiraph::girafe(code = print(.))
to add some interactivity.Update 2 - change labels
The OP wants the labels to be the same in the static and interactive plots.
The labels for both the static and interactive plots are stored inside
<the plot object>$plot_env
. From here it's just a matter of looking around, and replacing the static labels with the interactive ones. Since the interactive labels contains HTML-tags, we do some cleaning first. I would wrap this in a function, as such:By adding this function to
make_plot()
we get the labels we want: