2017-07-31 83 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