【发布时间】:2020-09-23 09:27:40
【问题描述】:
我有两个data.frame如下:
df1 <- data.frame(
week = c(rep(1, times = 3), rep(2, times = 3)),
cat = rep(c("A", "B", "C"), times = 2),
count = round(runif(6, 1, 10))
)
和
df2 <- data.frame(
week = c(rep(1, times = 2), rep(2, times = 2)),
cat = rep(c("E", "F"), times = 2),
count = round(runif(4, 1, 10))
)
情节代码是
plotly::plot_ly(df1,x = ~week, y = ~count, type = "bar", split = ~cat) %>%
add_trace(data = df2,x = ~week, y = ~count, type = "scatter",
mode = "lines", split = ~cat, yaxis = 'y2') %>%
layout(title = 'Title',
xaxis = list(title = ""),
yaxis = list(side = 'left', title = 'var1', showgrid = FALSE, zeroline = FALSE),
yaxis2 = list(side = 'right', overlaying = "y",
title = 'var2', showgrid = FALSE, zeroline = FALSE),
showlegend = FALSE)
但我无法为每个组(A、B、C、E 和 F)指定特定颜色。我试图将color = ~cat, colors = c(A = "red", B = "green", C = "yellow") 用于条形图,同样用于线图。但不知何故,如果我只使用条形图或线图,它就不起作用了。
谢谢。
【问题讨论】: