0

I am using Plotly to plot actual versus predicted values from a dataset. Everything runs fine, except I would like to color actual value points (SalePrice) in blue and predicted value points (pred.price) in red. I'm relatively new to Plotly and have been playing around with some examples given online but it's not quite what I'm looking for. I have the following syntax below that plots all points in a singular color but I am not sure how to proceed. Thank you

avp.plot <- plot_ly(data = avp.df, x = ~SalePrice, y = ~pred.price, type = 'scatter')
avp.plot

The head of the df looks like this:

  SalePrice pred.price

1   208500  206900.5        
2   181500  175133.0        
3   223500  216843.6        
4   140000  216189.2        
5   250000  281824.2        
6   143000  135957.3    
2
  • 2
    Can you provide some sample data? If you have a column with actual and another with predicted values you can adjust the colours manually. But without knowing the data structure this is difficult to answer Commented Apr 22, 2021 at 17:20
  • @Albin - Edited with example Commented Apr 22, 2021 at 18:59

1 Answer 1

1

In my understanding you still need the x-axis defined. I did this with the column id.

Here is the provided data which I took from your example:

avp.df <- data.frame(id = c(1,2,3,4,5,6), 
                   SalePrice = c(208500, 181500, 223500, 140000, 250000, 143000), 
                   pred.price = c(206900.5, 175133.0, 216843.6, 216189.2, 281824.2, 135957.3))

There are different ways to create a plotly scatter plot. Taken this example you need to define your x and y value. x is defined as the id and SalePrice and pred.price as the y-axis values.

fig <- plot_ly(data = avp.df, x= ~id, y = ~SalePrice, name = 'Sale Price', type = 'scatter', mode = 'markers', marker=list(color='rgb(255,0,0)'))
fig <- fig %>% add_trace(y = ~pred.price, name = 'Predicted Price', mode = 'markers', marker=list(color='rgb(0,0,255)'))

fig

And you will get following scatter plot:

Plotly visualization

For additional adjustments I recommend the official plotly homepage.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.