R 绘图叠加条形图 [英] R Plotly overlay bar chart
问题描述
简而言之问题:使用 R
和 Plotly
包,我可以创建一个覆盖条形图,其中使用 x 轴上的相同位置显示 2 个系列吗?在谷歌上搜索了很多之后,我找不到答案.
Question in short: using R
and the Plotly
package, can I create an overlay bar chart where 2 series are shown using the same position on the x-axis? After quite a bit of Googling, I could not find the answer.
例如这个可视化:
使用 Plotly 和 R 创建分组(未覆盖)堆叠条的代码:
Code to create a grouped (not-overlayed) stacked bar, using Plotly and 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')
如何调整图表以便系列叠加?关于如何以类似的方式可视化相同信息但具有不同逻辑的建议也非常感谢!
How can I tweak the chart so that the series overlay? Suggestions on how to visualize the same information in a similar way, but with a different logic are also very much appreciated!
推荐答案
一种使用 plotly
和开发版 ggplot2
One way to do it using plotly
and the development version of 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)
<小时>
这篇关于R 绘图叠加条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!