[英]Stacked Bar Chart with Line Chart not working in R with plotly
I'm trying top prepare stacked bar chart with line chart and two y axis in R using plotly, but line part is not displaying. 我正在尝试使用plotly在R中准备带有折线图和两个y轴的堆叠条形图,但折线部分未显示。 Both y axis are fine, stacked bar chart also works.
y轴都很好,堆积条形图也可以。 Line chart by itself also works, but not together with stacked bar chart.
折线图本身也可以使用,但不能与堆积的条形图一起使用。 When I tried it with only one y axis chart had all components, but because of different scale it's not visualizing data well enough.
当我仅用一个y轴图表进行尝试时,它具有所有组件,但是由于比例不同,它无法很好地可视化数据。 Here is code with sample data:
这是带有示例数据的代码:
library(plotly)
#data
big <- c(300000,400000,500000,600000,500000,600000)
v1 <- c(3,4,5,5,4,3)
v2 <-c(3,4,5,5,4,3)
Date <- c("Jan 2016","Feb 2016","Mar 2016","Apr 2016","May 2016","June 2016")
df <- data.frame(big, v1, v2, Date)
#plot
p1 <- plot_ly(
x = df$Date,
y = df$big,
type="scatter"
)
p2 <- add_trace(
p1,
x = df$Date,
y = df$v2,
type = "bar",
yaxis="y2")
p25 <- add_trace(
p2,
x = df$Date,
y = df$v1,
type = "bar",
yaxis="y2"
)
p3 <- layout(p25,
xaxis = list(
title = "Month"
),
yaxis = list(
title = "big"
),
yaxis2=list(
title = "little",
tickfont = list(color = "red"),
overlying="y",
side="right"
),
barmode="stack"
)
p3
Any idea how to correct it? 知道如何纠正吗?
This worked for me: 这对我有用:
# data
big <- c(300000,400000,500000,600000,500000,600000)
v1 <- c(3,4,5,5,4,3)
v2 <-c(3,4,5,5,4,3)
Date <- c("Jan 2016","Feb 2016","Mar 2016","Apr 2016","May 2016","June 2016")
df <- data.frame(big, v1, v2, Date)
library(plotly)
df %>%
plot_ly(x = ~Date) %>%
add_bars(y = ~v1,
name = "bar1") %>%
add_bars(y = ~v2,
name = "bar2") %>%
add_lines(y = ~big,
name = "line",
yaxis = "y2") %>%
layout(barmode = "stack",
yaxis2 = list(overlaying = "y",
side = "right"),
barmode = "stack")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.