r - R:使用 Plotly() 的水平堆积条形图
问题描述
我正在尝试使用 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"
)
推荐阅读
- dfa - DFA 可以有一个带有空字符串的箭头作为输入吗?
- c++ - 函数是否在返回值后立即终止,忽略其下一行?
- flutter - Flutter IconTheme 不适用
- windows - 聪明的。卡微型驱动程序测试库加载失败 - cmck
- jenkins - 如何简单地执行键的值,其中值是元素列表,每个元素执行应该在jenkins groovy中并行执行
- reactjs - (错误)AWS 作为后端安装放大库
- discord - 链接到 Discord.py Meme 命令中的 meme
- c# - JSON模型的多重继承的替代方案?
- laravel - 动态字段验证 (Livewire) - 有效但不能引用错误桶
- r - 为什么在 R 中总结线性模型并没有显示所有需要的级别?