I am quite new to R and really new in plotly
.
I am trying to plot a quadratic (i.e. 2nd-degree polynomial) regression line. Once some prices vs years, and once the same prices vs a list of certain integer numbers (which can be the same), let's say scores. The data in this example are
price = c(995, 675, 690, 600, 612, 700, 589, 532, 448, 512, 537, 560)
score = c(89, 91, 88, 89, 91, 91, 89, 93, 83, 91, 91, 90)
year = c(2005:2016)
The first fit works well by coding
enter code here
qfit1 <- lm(price ~ poly (year,2))
and then a plotly with
add_trace(x=year, y=fitted(qfit1), type="scatter",
mode="lines", line=list(shape="spline"),)
producing this plot:
However, the second fit doesn't work:
qfit2 <- lm(price ~ poly (score,2))
p <- plot_ly() %>% ...
add_trace(x=score, y=fitted(qfit2), type="scatter", mode="lines",
line=list(shape="spline", smoothing=1.3))*
gives me:
which links the 12 data values I have by curve lines. I then ordered the data so that the line that links the 12 values would be continuous by
add_trace(x=sort(score), y=fitted(qfit2)[order(score)],
type="scatter", mode="lines",
line=list(shape="spline", smoothing=1.3))*
but again the result is not what I want:
The line produced is not smooth at all, it basically links the 12 values with curve lines, and what I noticed (of course I produced more similar graphs with different data) was that the problem always happens when a certain score (x-axis) has various prices. However, I can't understand how to solve that. Any idea on that? Or maybe anyone knowing a different way of producing a quadratic fit line using R and plotly? (I also tried to use add_lines instead of add_trace, but that gave me an even worse result)
Thank you very much in advance.