【问题标题】:ggplot2 stacked bar with negative values not working with Plotly带有负值的ggplot2堆积条不适用于Plotly
【发布时间】:2017-01-20 16:34:40
【问题描述】:

我在上一个帖子ggplot2 and a Stacked Bar Chart with Negative Values 中尝试了答案中的代码。

dat <- read.table(text = "   Division Year OperatingIncome
1  A  2012           11460
  2  B  2012            7431
  3  C  2012           -8121
  4  D  2012           15719
  5  E  2012             364
  6  A  2011           12211
  7  B  2011            6290
  8  C  2011           -2657
  9  D  2011           14657
  10 E  2011            1257
  11 A  2010           12895
  12 B  2010            5381
  13 C  2010           -2408
  14 D  2010           11849
  15 E  2010             517",header = TRUE,sep = "",row.names = 1)

dat1 <- subset(dat,OperatingIncome >= 0)
dat2 <- subset(dat,OperatingIncome < 0)
plot <- ggplot() + 
  geom_bar(data = dat1, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") +
  geom_bar(data = dat2, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") +
  scale_fill_brewer(type = "seq", palette = 1)
ggplotly(plot)

这是我得到的:

如果我运行plot(plot),那么它工作正常:

如何解决 Plotly 中的问题?

【问题讨论】:

  • 试试 aes(x=as.factor(Year)....
  • 它没有用。它只是将我的情节标签更改为 as.factor(Year)
  • 好像是已知的issue
  • 我也在使用 plotly 4.5.6。奇怪。

标签: r ggplot2 plotly


【解决方案1】:

面向未来的读者

现在,plotly(我正在使用4.8.0)支持带有负值的堆叠条形图。在layout 中,您必须设置barmode=relative。此外,您还可以使用问题中发布的ggplotly 功能。

plot_ly(dat, y=~OperatingIncome, x=~Year, type='bar', name=~Division, color =~Division, 
        colors='Blues', marker=list(line=list(width=1, color='lightgray'))) %>% 
  layout(barmode = 'relative')

将返回:

【讨论】:

    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多