4

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:

enter image description here

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:

enter image description here

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:

enter image description here

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.

3
  • could you provide the data to go along with the code. With dput preferably, and paste it to your question. Commented Mar 26, 2018 at 12:14
  • For this certain example, I have: 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) and year = c(2005:2016). Does that help? Commented Mar 26, 2018 at 12:28
  • 1
    Thanks, added answer, check please. Could you put that data in your original post by using edit. Commented Mar 26, 2018 at 12:41

2 Answers 2

3

Here is a working code to plot a fitted model in plotly:

  library(plotly)
  library(dplyr)
  data(cars, package = "datasets")

 qfit1 <- lm(dist ~ poly(speed,2), data = cars)

  cars %>%     
  plot_ly() %>%  
  add_lines(x = ~speed, y = fitted(qfit1)) %>%
  add_trace(x=~speed, y=~dist)

enter image description here

the line is not so smooth since there are few fitted points. To make a smoother line create new data:

  dat <- data.frame(speed = (1:300)/10,
                    dist = predict(qfit1, data.frame(speed = (1:300)/10)))
  plot_ly() %>% 
      add_trace(x=~speed, y=~dist, type="scatter", mode="lines", data = dat) %>%
      add_trace(x=~speed, y=~dist, type="scatter", data = cars)

enter image description here

With data from the comment:

 dat1 = data.frame(
       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))

qfit2 <- lm(price ~ poly (score,2), data = dat1)

  dat3 <- data.frame(score = (800:950)/10,
                    price = predict(qfit2, data.frame(score = (800:950)/10)))

plot_ly() %>% 
   add_trace(x=~score, y=~price, type="scatter", mode="lines", data = dat3) %>%
   add_trace(x=~score, y=~price, type="scatter", data = dat1)

enter image description here

Problem is your fitted values are sparse and uneven, so you need to predict on new data that is evenly spaced to get a nice looking curve.

Sign up to request clarification or add additional context in comments.

Comments

0

You could also use ggplot2 and ggplotly to get what you want. Try this:

library(plotly)
library(ggplot2)
data_df <- data.frame(price, score, year)
p <- ggplot(data_df, aes(x=score, y=price)) + 
  geom_point() + 
  geom_smooth(method="lm", se=FALSE, fill=NA, formula=y ~ poly(x, 2, raw=TRUE),colour="blue") + 
  theme_bw() 
ggplotly(p)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.