从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”然后我的代码完美无缺。但我真的需要调整图表的条宽。有没有人知道这个解决方案?
您可以使用ggplotly
作为替代方案。你可以试试这个:
ggplotly(ggplot(data = data, aes(x = x, y = y, fill = group, width = width)) + geom_col() + theme_bw())