繁体   English   中英

使用 Plotly() 的水平堆积条形图

[英]Horizontal stacked bar chart using Plotly()

我正在尝试使用 plotly 来 plot 水平堆叠条形图。 但即使layout(barmode = 'stack')使用orientation = "h"指定,我也会得到水平条形图

这是我需要 plot 作为堆叠图的数据

    Var1 Freq percentage
1 Tool 1  104         35
2 Tool 2   81         28
3 Tool 3   36         12
4 Tool 4   30         10
5 Tool 5   23          8
6 Tool 6   10          3
7 Tool 7    8          3
8 Tool 8    2          1

这是用于 plot 的代码 图表使用plotly

plot_ly(tooldf, x = tooldf$percentage,
        y = tooldf$Var1, 
        type = 'bar', orientation = "h",
        name = tooldf$Var1,
        text = paste(tooldf$percentage,"%"),
        textposition = 'top',
        hoverinfo = 'text',
        hovertext = paste('Tool: ', tooldf$Var1,
                          '<br> % of Usage: ', paste(tooldf$percentage,"%"))) %>%
  layout(barmode = 'stack')

谁能帮我 plot R 中的水平堆叠图表?

您缺少另一个变量。 现在你只是通过 x 和 y 轴,你想如何堆叠它? 我在下面介绍了一个名为Type的新虚拟变量。

tooldf <- data.frame(Var1 = paste0("Tool", 1:8),
                     Freq = c(2, 8, 10, 23, 30, 36, 81, 104),
                     percentage = c(1, 3, 3, 8, 10, 12, 28, 35))

tooldf <- tooldf %>%
            mutate(Type = Freq > mean(Freq))

library(plotly)

tooldf %>%
  plot_ly(
    x = ~percentage
    ,y = ~Type
    ,color = ~Var1
    ,name = ~Var1
    ,type = "bar"
    ,orientation = "h"
  ) %>%
  layout(
    barmode = "stack"
  )

在此处输入图像描述

暂无
暂无

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

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