在ggplot()
中,我可以动态更改lwd
中的aes
变量。但是,如果我改为使用plot_ly()
,则此选项不可用。
我也知道有ggplotly
将ggplot转换为情节,但在ggplotly中使用geom_line()
时会出现问题,如此问题所示:ggplotly not displaying geom_line correctly
我目前在R中使用plotly,但如果需要,可以将我的代码库转换为python。
答案 0 :(得分:1)
@Adam_G见下文。这是很糟糕的,所以可能有更好的方法来做到这一点。比我更了解情报的人可能会对此有所了解。
library(plotly)
library(dplyr)
ds <- data.frame(x = 1:100,
y = sample(1:1000, size = 100),
group = sample(LETTERS[1:3], size = 100, replace = T))
# Default
plot_ly(ds, x = x, y = y, group = group, mode = "lines")
# Manually change linewidth (if not too many groups)
plot_ly(ds %>% filter(group == "A"), x = x, y = y, mode = "lines", line = list(width = 2)) %>%
add_trace(data = ds %>% filter(group == "B"), x = x, y = y, mode = "lines", line = list(width = 5)) %>%
add_trace(data = ds %>% filter(group == "C"), x = x, y = y, mode = "lines", line = list(width = 10))
# In a loop (if too many groups)
p <- plot_ly()
vec <- unique(ds$group)
widths <- c(2, 5, 10)
for(i in 1:3){
# Note that evaluate = T is important here
p <- add_trace(p, data = ds %>% filter(group == vec[i]), x = x, y = y, mode = "lines",
line = list(width = widths[i]), evaluate = T)
}
p