简体   繁体   English

R plotly 带子图的饼图

[英]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.我正在尝试制作一个R plotly pie chart subplot ,其中包含两个饼图,每个饼图都有一个标题。 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:我遵循了本教程Subplots下的示例,只绘制了三个饼图中的两个并为每个饼图添加了一个标题:

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:这使: 在此处输入图像描述

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.它看起来类似于使用plotly subplot function 时出现多个标题的问题(在此处讨论),但在这种情况下未明确调用子图,因此我不确定此处的等效解决方案是什么。

The title argument needs to be in the trace call: title参数需要在trace调用中:

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)))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM