设置绘图的附加平均线的y极限

时间:2019-08-16 16:40:49

标签: r plotly

我已经创建了一个基本的填充图,并且我尝试添加一条平均线:

library(plotly)
week<-c(2,1,3)
pts<-c(10,20,30)
wex<-data.frame(week,pts)
wex <- wex[order(wex$week), ]

plot_ly(x = ~wex$week, y = ~wex$pts, type = 'scatter', mode = 'lines', 
        fill = 'tozeroy')%>%
add_segments(x = 0, xend = max(wex$week), y = mean(wex$pts), yend =mean(wex$pts) )

但是我得到了一个完整的区域,而不是一行。我猜想它与yend参数有关,但没有意义,因为它与y参数相同。

1 个答案:

答案 0 :(得分:1)

似乎fill='tozeroy'被保留用于add_segments()呼叫。

这有效:

library(plotly)
week<-c(2,1,3)
pts<-c(10,20,30)
wex<-data.frame(week,pts)
wex <- wex[order(wex$week), ]
wex
plot_ly(x = ~wex$week, y = ~wex$pts, type = 'scatter', mode = 'lines', 
        fill = 'tozeroy')%>%
  add_segments(x = 0, xend = max(wex$week), y = mean(wex$pts), yend =mean(wex$pts),fill = 'none' )

我在fill = 'none'中添加了add_segments()