Load plotly and US arrests dataset
library(plotly)
## Warning: package 'plotly' was built under R version 3.5.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.5.2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
data(USArrests)
Plot murder arrests versus assault arrests with population size per state
df <- data.frame(USArrests)
df <- cbind(df, states=row.names(df))
x <- list(
title = "Assualt arrests"
)
y <- list(
title = "Murder arrests"
)
plot_ly(df, x = ~Assault, y = ~Murder,
color = "blue", size = ~UrbanPop, name = ~states) %>%
layout(xaxis = x, yaxis = y)
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
Plot murder arrests per capita versus assault arrests per capita with population size per state
df$murders_per_capita <- df$Murder / df$UrbanPop
df$assualts_per_capita <- df$Assault / df$UrbanPop
x <- list(
title = "Assualt arrests per capita"
)
y <- list(
title = "Murder arrests per capita"
)
p <- plot_ly(df, x = ~assualts_per_capita, y = ~murders_per_capita,
color = "blue", size = ~UrbanPop, name = ~states) %>%
layout(xaxis = x, yaxis = y)
p
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels