Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R绘图叠加条形图_R_Plotly - Fatal编程技术网

R绘图叠加条形图

R绘图叠加条形图,r,plotly,R,Plotly,简言之,问题:使用R和Plotly软件包,我可以创建一个叠加条形图,其中两个系列在x轴上使用相同的位置显示吗?在谷歌搜索了好一会儿之后,我找不到答案 例如,此可视化: 使用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 [,

简言之,问题:使用
R
Plotly
软件包,我可以创建一个叠加条形图,其中两个系列在x轴上使用相同的位置显示吗?在谷歌搜索了好一会儿之后,我找不到答案

例如,此可视化:

使用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')


如何调整图表以使序列重叠?关于如何以类似方式可视化相同信息,但使用不同逻辑的建议也非常受欢迎

使用
plotly
ggplot2

#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)
#devtools::install_github('hadley/ggplot2'))
图书馆(GG2)

p使用选项添加布局
barmode='overlay'
并更改其中一个数据集的宽度

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', name = "n1") %>%
    add_trace(x = dfPlot[,1],
        y = dfPlot[,3],
        type = 'bar', width = 0.3, name = "n2") %>%
  layout(barmode = 'overlay')

您是否愿意使用
ggplotly
解决方案?感谢您的快速回复@StevenBeaupré。据我所知(从未测试过),输出几乎相似,对吗?在这种情况下,我想看看如何。