dan
dan

Reputation: 6314

R plotly pie chart with subplots

I'm trying to produce an R plotly pie chart subplot with two pies with a title for each. I followed the example under the Subplots header in this tutorial, only plotting two out of the three pies and adding a title to each:

library(plotly)
library(dplyr)

fig <- plot_ly()
fig <- fig %>% add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n, textinfo='label+percent', showlegend=F,
                       name = "Cut", domain = list(x = c(0, 0.4), y = c(0.4, 1))) %>%
  layout(title="Cut")
fig <- fig %>% add_pie(data = count(diamonds, color), labels = ~color, values = ~n, textinfo='label+percent', showlegend=F,
                       name = "Color", domain = list(x = c(0.6, 1), y = c(0.4, 1))) %>%
  layout(title="Color")

Which gives: enter image description here

It looks similar to the problem of having multiple titles when using the plotly subplot function (discussed here), but in this case subplot is not explicitly called so I'm not sure what would be the equivalent solution here.

Upvotes: 0

Views: 1256

Answers (1)

dan
dan

Reputation: 6314

The title argument needs to be in the trace call:

fig <- plot_ly()
fig <- fig %>% add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n, textinfo='label+percent', showlegend=F, title="Cut",
                       name = "Cut", domain = list(x = c(0, 0.4), y = c(0.4, 1)))
fig <- fig %>% add_pie(data = count(diamonds, color), labels = ~color, values = ~n, textinfo='label+percent', showlegend=F, title="Color",
                       name = "Color", domain = list(x = c(0.6, 1), y = c(0.4, 1)))

enter image description here

Upvotes: 2

Related Questions