【问题标题】:Plotly equivalent of ggplot's lwdPlotly 相当于 ggplot 的 lwd
【发布时间】:2016-08-16 06:37:50
【问题描述】:

ggplot() 中,我可以动态更改aes 中的lwd 变量。但是,如果我改用plot_ly(),则此选项不可用。

我也知道 ggplotly 将 ggplot 转换为 plotly,但在 ggplotly 中使用 geom_line() 存在问题,如以下问题所示:ggplotly not displaying geom_line correctly

我目前在 R 中使用 plotly,但如有必要,可以将我的代码库转换为 python。

【问题讨论】:

  • 为什么不赞成/投票结束?
  • 我知道你可以通过设置marker = list(size = ...) 来为标记做到这一点,但我认为线条不可能。至少不像ggplot() 中的aes。使用 plot_ly() 时,您可以通过将每个组添加为单独的跟踪来破解它
  • 谢谢。你这是什么意思?

标签: python r plot ggplot2 plotly


【解决方案1】:

@Adam_G 见下文。它有点像hack-y,所以可能有更好的方法来做到这一点。比我知识渊博的人也许能够对此有所了解。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多