sách gpt4 ai đã đi

r - 如何使用 plotly subplots() 删除重复的图例条目

In lại 作者:行者123 更新时间:2023-12-04 02:16:24 26 4
mua khóa gpt4 Nike

使用 plotly 的 subplots() 时,如何删除图例中的重复项?

这是我的 MWE:

library(plotly)
library(ggplot2)
library(tidyr)

mpg %>%
group_by(class) %>%
do(p = plot_ly(., x = ~cyl, y = ~displ, color = ~trans, type = 'bar')) %>%
subplot(nrows = 2, shareX = TRUE, titleX = TRUE) %>%
layout(barmode = 'stack')

1 Câu trả lời

sử dụng tidyverse 的另一种解决方法。原始 MWE 添加了以下步骤:

  • Sẽ trans 列转换为因子。
  • 使用 tidyr 的 complete 为每个 lớp học 组中的缺失因子水平填充(非 NA)虚拟值。
  • 按照 M-M 的建议设置 showlegend đến TRUE 单个组和 legendgroup 到 0x25184312 条目之间的子图链接。

  • library(plotly)
    library(tidyverse)

    mpg %>%
    mutate_at("trans", as.factor) %>%
    group_by(class) %>%
    group_map(.f = ~{
    ## fill missing levels w/ displ = 0, cyl = first available value
    complete(.x, trans, fill = list(displ = 0, cyl = head(.x$cyl, 1))) %>%
    plot_ly(x = ~cyl, y = ~displ, color = ~trans, colors = "Paired", type = "bar",
    showlegend = (.y == "2seater"), legendgroup = ~trans) %>%
    layout(yaxis = list(title = as.character(.y)), barmode = "stack")
    }) %>%
    subplot(nrows = 2, shareX = TRUE, titleY = TRUE)

    plotly_plot

    关于r - 如何使用 plotly subplots() 删除重复的图例条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57253488/

    26 4 0
    行者123
    Hồ sơ cá nhân

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá Didi Taxi miễn phí
    Mã giảm giá Didi Taxi
    Giấy chứng nhận ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com