繁体   English   中英

以R设置的方式绘制单独堆积的条形宽度

[英]Plotly in R Set Individual Stacked Bar Width

在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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM