[英]Horizontal stacked bar chart using Plotly()
I'm trying to plot a horizontal stacked bar chart using plotly.我正在尝试使用 plotly 来 plot 水平堆叠条形图。 But I'm getting the horizontal bar chart even when
layout(barmode = 'stack')
is specified with orientation = "h"
但即使
layout(barmode = 'stack')
使用orientation = "h"
指定,我也会得到水平条形图
This is the data I need to plot as stacked chart这是我需要 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
This is the code used to plot the chart using plotly
这是用于 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')
Can anyone help me to plot a horizontal stacked chart in R?谁能帮我 plot R 中的水平堆叠图表?
You're missing another variable.您缺少另一个变量。 Right now you're only passing through x and y axis, how do you want to stack it?
现在你只是通过 x 和 y 轴,你想如何堆叠它? I have introduced a new dummy variable called
Type
below.我在下面介绍了一个名为
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.