You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为组合的两个Donut Plot配置独立图例?

为每个环形图设置独立图例的解决方案

问题描述

我正在尝试将两个Donut Plot合并到一个图形中。当前生成的结果是两个图共用一个右侧图例,但我需要为每个Donut Plot设置独立的图例。以下是我的实现代码:

library(plotly)
library(dplyr)

# data
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
df <- mtcars %>% 
      group_by(am, manuf) %>% 
      summarize(count = n())

# Donut 1
fig1 <- plot_ly(df %>% 
                  count(am, manuf) %>% 
                  filter(am == 0), labels = ~manuf, values = ~n, type = 'pie', hole = 0.6,
                domain = list(x = c(0, 0.48), y = c(0, 1))) %>%
        layout(
          title = "Donut Chart 1",
          showlegend = FALSE,
          xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
          yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
        )

# Donut 2
fig2 <- plot_ly(df %>% 
                  count(am, manuf) %>% 
                  filter(am == 1),
                  labels = ~manuf, values = ~n, type = 'pie', hole = 0.6,
                domain = list(x = c(0.52, 1), y = c(0, 1))) %>%
        layout(
          title = "Donut Chart 2",
          showlegend = TRUE,
          xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
          yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
        )

facet_fig <- subplot(fig1, fig2, nrows = 1, shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE)
facet_fig

解决方法

Plotly默认会合并子图的图例,要实现独立图例,需要通过图例分组和位置配置来实现。修改后的完整代码如下:

library(plotly)
library(dplyr)

# 数据处理
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
df <- mtcars %>% 
      group_by(am, manuf) %>% 
      summarize(count = n())

# 第一个环形图
fig1 <- plot_ly(df %>% 
                  count(am, manuf) %>% 
                  filter(am == 0), 
                labels = ~manuf, 
                values = ~n, 
                type = 'pie', 
                hole = 0.6,
                domain = list(x = c(0, 0.48), y = c(0, 1)),
                legendgroup = "group1",  # 标记图例组1
                name = "Donut 1") %>%  # 图例组名称
        layout(
          title = list(text = "Donut Chart 1", x = 0.24, xanchor = "center"),
          showlegend = TRUE,  # 开启该子图的图例显示
          xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
          yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
        )

# 第二个环形图
fig2 <- plot_ly(df %>% 
                  count(am, manuf) %>% 
                  filter(am == 1),
                labels = ~manuf, 
                values = ~n, 
                type = 'pie', 
                hole = 0.6,
                domain = list(x = c(0.52, 1), y = c(0, 1)),
                legendgroup = "group2",  # 标记图例组2
                name = "Donut 2") %>%  # 图例组名称
        layout(
          title = list(text = "Donut Chart 2", x = 0.76, xanchor = "center"),
          showlegend = TRUE,  # 开启该子图的图例显示
          xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
          yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE)
        )

# 合并子图并配置独立图例位置
facet_fig <- subplot(fig1, fig2, nrows = 1, shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE) %>%
  layout(
    legend = list(
      # 第一个图例(对应group1)放在左侧
      list(
        x = -0.1,
        y = 0.5,
        tracegroupgap = 0,
        title = list(text = "Donut 1<br>")
      ),
      # 第二个图例(对应group2)放在右侧
      list(
        x = 1.1,
        y = 0.5,
        tracegroupgap = 0,
        title = list(text = "Donut 2<br>")
      )
    )
  )

facet_fig

关键修改点

  • 给每个环形图添加legendgroup参数,将图例分成独立的两组
  • 开启每个子图的showlegend = TRUE,确保各自图例被渲染
  • 在合并后的布局中,通过legend参数定义两个独立图例,设置不同的x坐标分别放在左侧和右侧,避免重叠
  • 调整子图标题的x位置,让标题居中显示在各自环形图上方

内容的提问来源于stack exchange,提问作者silent_hunter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:53:14