在Plotly教程中,我知道如何设置单个条形宽度:
library(plotly)
x= c(1, 2, 3, 5.5, 10)
y= c(10, 8, 6, 4, 2)
width = c(0.8, 0.8, 0.8, 3.5, 4)
data <- data.frame(x, y, width)
p <- plot_ly(data) %>%
add_bars(
x= ~x,
y= ~y,
width = ~width
)
它将产生各种宽度:
但是,当我尝试使用堆积的条形图复制它时,它不起作用:
x = c(1, 2, 3, 5.5, 10,1, 2, 3, 5.5, 10)
y = c(10, 8, 6, 4, 2,0,2,4,6,8)
width = c(0.8, 0.8, 0.8, 3.5, 4,0.8, 0.8, 0.8, 3.5, 4)
group = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B")
data <- data.frame(x, y, width, group)
p <- data %>%
group_by(group) %>%
plot_ly(
x= ~x,
y= ~y,
width = ~width,
color = ~group,
colors = 'Reds',
type = "bar"
) %>%
layout(barmode = 'stack')
出现错误:
> print(p)
Error in validateCssUnit(sizeInfo$width) :
CSS units must be a single-element numeric or character vector
In addition: Warning message:
In is.na(x) : is.na() applied to non-(list or vector) of type 'language'
当然,注释掉“ width =〜width”,然后我的代码就可以正常工作了。但是我确实需要调整图表的条形宽度。有谁知道解决方案吗?
答案 0 :(得分:0)
您可以使用ggplotly
作为替代。您可以尝试以下方法:
ggplotly(ggplot(data = data, aes(x = x, y = y, fill = group, width = width)) + geom_col() + theme_bw())