ggplot plotly API 混乱宽度堆栈条形图

ggplot plotly API mess width stack bar graph

我正在使用 plotly 库来获取 HTML 交互式图形,我已经从 ggplot2 生成了它,但是对于堆叠图形,plotly 无法正常工作。

这是我的 ggplot 代码:

if(file.exists(filename)) {
  data = read.table(filename,sep=",",header=T)
} else {
  g <- paste0("=== [E] Error : Couldn't Found File : ",filename)
  print (g)
}
  ReadChData <- data[data$Channel %in% c("R"),]
  #head(ReadChData,10)

# calculate midpoints of bars (simplified using comment by @DWin)
Data <- ddply(ReadChData, .(qos_level), 
   transform, pos = cumsum(AvgBandwidth) - (0.5 *AvgBandwidth)
)

# library(dplyr) ## If using dplyr... 
# Data <- group_by(Data,Year) %>%
#    mutate(pos = cumsum(Frequency) - (0.5 * Frequency))

# plot bars and add text
g <- ggplot(Data, aes(x = qos_level, y = AvgBandwidth)) +
     scale_x_continuous(breaks = x_axis_break) +
     geom_bar(aes(fill = MasterID), stat="identity", width=0.2) +
     scale_colour_gradientn(colours = rainbow(7)) +
     geom_text(aes(label = AvgBandwidth, y = pos), size = 3) +
     theme_set(theme_bw()) +
     ylab("Bandwidth (GB/s)") +
     xlab("QoS Level") +
     ggtitle("Qos Compting Stream")

png(paste0(opt$out,"/",GraphName,".png"),width=6*ppi, height=6*ppi, res=ppi)
print (g)

library(plotly)
p <- ggplotly(g)
#libdir arugumet will be use to point to commin lib
htmlwidgets::saveWidget(as.widget(p), selfcontained=FALSE, paste0(opt$out,"/qos_competing_stream.html"))

这里是 HTML 输出形式 plotly lib

http://pasteboard.co/2fHQfJwFu.jpg

请帮忙。

回答这个问题可能有点晚了。但是对于将来可能遇到问题的人...

geom_bar 的宽度参数无法被 ggplotly 函数识别。

  1. 解决方法: 使用参数 colour="white", size = 1 的解决方法(不是很好)。这基本上在条形图周围添加了一条白线,产生类似白色 space 的效果。 您可以尝试以下操作: stat_summary(aes(fill = MasterID), geom="bar", colour="white", size = 1, fun.y = "sum", position = "stack")

  2. 更好的解决方案: 使用 layout 函数中的 bargap 参数。代码应该是: ggplotly(type='bar', ...) %>% layout(bargap = 3, autosize=T)

P.S。有问题的代码不可执行,由于缺少文件名而引发错误。