library(datasets)
data(mtcars)
names(mtcars)
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
[11] "carb"
library(ggplot2)
library(plotly)
set.seed(123)
plot_ly(mtcars,x=mtcars$wt,y=mtcars$mpg,type="scatter")
table(mtcars$cyl)
4 6 8
11 7 14
#Change the color of a factor in plotly
plot_ly(mtcars,x=mtcars$wt,y=mtcars$mpg,mode="markers",color=as.factor(mtcars$cyl))
#Equating color to a continous variable
plot_ly(mtcars,x=mtcars$wt,y=mtcars$mpg,mode="markers",color=mtcars$disp)
#Plotly treats it as a continous color gradient
#Change the size of the points in a scatterplot
plot_ly(mtcars,x=mtcars$wt,y=mtcars$mpg,mode="markers",color=as.factor(mtcars$cyl),size=mtcars$hp)
#hp is a continous variable