2016-07-29 65 views
0

我正在使用plotly庫讓我HTML交互式圖形,我已經從ggplot2生成,但與堆積圖形,情節不能正常工作。ggplot plotly API混亂寬度堆棧條形圖

這裏是我的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")) 

,這裏是plotly HTML輸出格式爲Lib

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

請幫助。

回答

3

這或許有點晚了。但對於將來可能有此問題的人...

geom_bar的寬度參數不被ggplotly函數識別。

  1. 解決方法: 一個解決(不是非常好)使用參數colour="white", size = 1。這基本上在酒吧周圍增加了一條白線,產生了像白色空間一樣的效果。 你可以嘗試以下方法: stat_summary(aes(fill = MasterID), geom="bar", colour="white", size = 1, fun.y = "sum", position = "stack")

  2. 更好的解決方案:使用 參數bargaplayout功能。該代碼應該是: ggplotly(type='bar', ...) %>% layout(bargap = 3, autosize=T)

附:問題代碼中的代碼不可執行,由於缺少文件名而引發錯誤。

+0

謝謝。我開始在offiline模式下使用plotly javascript而不是從ggplot對象生成。但是會接受答案。 – vjain419