Reputation: 43
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
Upvotes: 0
Views: 999
Reputation: 902
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:
For additional adjustments I recommend the official plotly homepage.
Upvotes: 1