2017-07-31 82 views
1

問題簡而言之:使用RPlotly包,我可以創建一個覆蓋條形圖,其中2個系列使用x軸上的相同位置顯示嗎?谷歌搜索了很多後,我找不到答案。R條紋覆蓋條形圖

例如該可視化:

enter image description here

代碼以創建分組(未疊加)堆積條形圖,使用Plotly和R:

months = 1:12 
n1 = runif(12, min = 0, max = 1) 
n2 = runif(12, min = 0, max = 1) 
dfPlot = data.frame(months, n1, n2) 

plot_ly(x = dfPlot [,1], 
     y = dfPlot [,2], 
     type = 'bar') %>% 
add_trace(x = dfPlot[,1], 
      y = dfPlot[,3], 
      type = 'bar') 

enter image description here

如何能我調整圖表,以便系列覆蓋?關於如何以相似的方式可視化相同的信息,但使用不同的邏輯的建議也非常感謝!

+0

開發版本做你開到使用'ggplotly'的解決方案? –

+1

感謝您的快速回復@StevenBeaupré。據我所知(儘管從未測試過),輸出結果幾乎相似嗎?那樣的話,想看看怎麼樣。 – Dendrobates

回答

2

一種方式,它利用plotlyggplot2

#devtools::install_github('hadley/ggplot2') 

library(ggplot2) 

p <- ggplot(dfPlot) + 
    geom_col(aes(x = month.abb[months], y = n1), 
      fill = "blue", width = 0.2) + 
    geom_col(aes(x = month.abb[months], y = n2), 
      alpha = 0.3, fill = "red", width = 0.6) + 
    labs(title = "Overlay geom_cols", x = "Months", y = "Measure") + 
    theme_minimal() 

plotly::ggplotly(p) 

enter image description here