簡體   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