我想在Plotly(R)中叠加2个条形图,但无法弄清楚。可以在ggplot2中轻松完成,但是qqplotly错误地渲染了它,因此我想在plot_ly中制作图表。感谢您的任何建议。
数据:
df = data.frame(
year = c(2014,2014,2014,2015,2015,2015,2016,2016,2016),
pet = c("dog","cat","bird","dog","cat","bird","dog","cat","bird"),
wt_before = c(56, 25, 26, 10, 19, 41, 16, 17, 13),
wt_after = c(49, 18, 19, 3, 12, 34, 9, 10, 6)
)
Run Code Online (Sandbox Code Playgroud)
ggplot:
ggplot(df)+
geom_bar(aes(year,wt_before,fill=pet),stat="identity",position="dodge",width = 0.9,alpha=0.5)+
geom_bar(aes(year,wt_after,fill=pet),stat="identity",position="dodge",width = 0.9)+
xlab("Year") +
ylab("Weight")
Run Code Online (Sandbox Code Playgroud)

阴谋尝试:
plot_ly(df,x= ~year) %>%
add_bars(y= ~wt_before, color = ~pet, alpha = 0.5) %>%
add_bars(y= ~wt_after, color = ~pet, showlegend=FALSE) %>%
layout(xaxis=list(title="Year"),
yaxis=list(title="Weight"))
Run Code Online (Sandbox Code Playgroud)
