firmo23
firmo23

Reputation: 8404

Set the y limits of an added average line of a plotly plot

I have created a basic plotly filled plot and I am trying to add an average line with:

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) )

but instead of a single line I get a whole area filled. I guess it has to do with yend argument but it does not make sense as it is the same as the y argument.

Upvotes: 2

Views: 287

Answers (1)

Chelmy88
Chelmy88

Reputation: 1116

It seems the fill='tozeroy' is kept for the add_segments() call.

This works:

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' )

I added fill = 'none' in add_segments()

Upvotes: 1

Related Questions