From Plotly tutorial I know how to set individual bar width like this:
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
)
And it will produce nice various width:
However, when I try to replicate this with stacked bar graphs, it doesn't work:
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')
It gives the error:
> 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'
Of course comment out "width = ~width" then my code works perfectly. But I really need to adjust bar width for my graph. Is there anyone knowing the solution to this?
You can use ggplotly
as an alternative. You could try this:
ggplotly(ggplot(data = data, aes(x = x, y = y, fill = group, width = width)) + geom_col() + theme_bw())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.