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

内维达(Nevedha Ayyanar)

我正在尝试使用plotly绘制水平堆积的条形图。但是即使使用layout(barmode = 'stack')来指定水平条形图orientation = "h"

这是我需要绘制为堆积图的数据

    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

这是用于使用以下方式绘制图表的代码 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')

谁能帮我在R中绘制水平堆积图?

仙人掌

您缺少另一个变量。现在,您仅穿过x和y轴,如何堆叠它?我介绍了一个称为Type以下的新虚拟变量

a <- "Var1 Freq percentage
Tool1  104         35
Tool2   81         28
Tool3   36         12
Tool4   30         10
Tool5   23          8
Tool6   10          3
Tool7    8          3
Tool8    2          1"

tooldf <- read.table(text = a, header = TRUE) 

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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章