diff --git a/plotly/plotly.Rmd b/plotly/plotly.Rmd index 9e65ab4..97c7d2d 100644 --- a/plotly/plotly.Rmd +++ b/plotly/plotly.Rmd @@ -50,13 +50,15 @@ You can add color to your scatterplot points according to a categorical variable in the data frame you use with `plot_ly()`. ```{r, eval=FALSE} -plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", color = ~factor(cyl)) +plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", + color = ~factor(cyl)) ``` ## Scatterplot Color ```{r, echo=FALSE, message=FALSE} -plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", color = ~factor(cyl)) +plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", + color = ~factor(cyl)) ``` ## Continuous Color @@ -64,13 +66,15 @@ plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", color = ~factor(cyl)) You can also show continuous variables with color in scatterplots. ```{r, eval=FALSE} -plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", color = ~disp) +plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", + color = ~disp) ``` ## Continuous Color ```{r, echo=FALSE, message=FALSE} -plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", color = ~disp) +plot_ly(mtcars, x = ~wt, y = ~mpg, type = "scatter", + color = ~disp) ``` ## Scatterplot Sizing @@ -123,14 +127,16 @@ showing change over time: ```{r, eval=FALSE} data("airmiles") -plot_ly(x = ~time(airmiles), y = ~airmiles, type = "scatter", mode = "lines") +plot_ly(x = ~time(airmiles), y = ~airmiles, + type = "scatter", mode = "lines") ``` ## Line Graph ```{r, echo=FALSE, message=FALSE} data("airmiles") -plot_ly(x = ~time(airmiles), y = ~airmiles, type = "scatter", mode = "lines") +plot_ly(x = ~time(airmiles), y = ~airmiles, + type = "scatter", mode = "lines") ``` ## Multi Line Graph @@ -148,7 +154,8 @@ stocks <- as.data.frame(EuStockMarkets) %>% gather(index, price) %>% mutate(time = rep(time(EuStockMarkets), 4)) -plot_ly(stocks, x = ~time, y = ~price, color = ~index, type = "scatter", mode = "lines") +plot_ly(stocks, x = ~time, y = ~price, color = ~index, + type = "scatter", mode = "lines") ``` ## Multi Line Graph @@ -163,7 +170,8 @@ stocks <- as.data.frame(EuStockMarkets) %>% gather(index, price) %>% mutate(time = rep(time(EuStockMarkets), 4)) -plot_ly(stocks, x = ~time, y = ~price, color = ~index, type = "scatter", mode = "lines") +plot_ly(stocks, x = ~time, y = ~price, color = ~index, + type = "scatter", mode = "lines") ``` ## Histogram @@ -187,13 +195,15 @@ Boxplots are wonderful for comparing how different datasets are distributed. Specify `type = "box"` to create a boxplot. ```{r, eval=FALSE} -plot_ly(iris, y = ~Petal.Length, color = ~Species, type = "box") +plot_ly(iris, y = ~Petal.Length, color = ~Species, + type = "box") ``` ## Boxplot ```{r, echo=FALSE, message=FALSE} -plot_ly(iris, y = ~Petal.Length, color = ~Species, type = "box") +plot_ly(iris, y = ~Petal.Length, color = ~Species, + type = "box") ``` ## Heatmap @@ -220,14 +230,16 @@ Why limit yourself to two dimensions when you can render three dimensions on a computer!? Create moveable 3D surfaces with `type = "surface"`. ```{r, eval=FALSE} -terrain2 <- matrix(sort(rnorm(100*100)), nrow = 100, ncol = 100) +terrain2 <- matrix(sort(rnorm(100*100)), nrow = 100, + ncol = 100) plot_ly(z = ~terrain2, type = "surface") ``` ## 3D Surface ```{r, echo=FALSE, message=FALSE} -terrain2 <- matrix(sort(rnorm(100*100)), nrow = 100, ncol = 100) +terrain2 <- matrix(sort(rnorm(100*100)), nrow = 100, + ncol = 100) plot_ly(z = ~terrain2, type = "surface") ``` @@ -239,9 +251,11 @@ require more setup compared to other Plotly graphics. ```{r, eval=FALSE} # Create data frame -state_pop <- data.frame(State = state.abb, Pop = as.vector(state.x77[,1])) +state_pop <- data.frame(State = state.abb, + Pop = as.vector(state.x77[,1])) # Create hover text -state_pop$hover <- with(state_pop, paste(State, '
', "Population:", Pop)) +state_pop$hover <- with(state_pop, paste(State, '
', + "Population:", Pop)) # Make state borders white borders <- list(color = toRGB("red")) # Set up some mapping options @@ -256,19 +270,24 @@ map_options <- list( ## Choropleth Maps: Mapping ```{r, eval=FALSE} -plot_ly(z = ~state_pop$Pop, text = ~state_pop$hover, locations = ~state_pop$State, - type = 'choropleth', locationmode = 'USA-states', - color = state_pop$Pop, colors = 'Blues', marker = list(line = borders)) %>% - layout(title = 'US Population in 1975', geo = map_options) +plot_ly(z = ~state_pop$Pop, text = ~state_pop$hover, + locations = ~state_pop$State, + type = 'choropleth', locationmode = 'USA-states', + color = state_pop$Pop, colors = 'Blues', + marker = list(line = borders)) %>% + layout(title = 'US Population in 1975', + geo = map_options) ``` ## Choropleth Maps ```{r, echo=FALSE, message=FALSE} # Create data frame -state_pop <- data.frame(State = state.abb, Pop = as.vector(state.x77[,1])) +state_pop <- data.frame(State = state.abb, + Pop = as.vector(state.x77[,1])) # Create hover text -state_pop$hover <- with(state_pop, paste(State, '
', "Population:", Pop)) +state_pop$hover <- with(state_pop, + paste(State, '
', "Population:", Pop)) # Make state borders white borders <- list(color = toRGB("red")) # Set up some mapping options @@ -279,10 +298,13 @@ map_options <- list( lakecolor = toRGB('white') ) -plot_ly(z = ~state_pop$Pop, text = ~state_pop$hover, locations = ~state_pop$State, - type = 'choropleth', locationmode = 'USA-states', - color = state_pop$Pop, colors = 'Blues', marker = list(line = borders)) %>% - layout(title = 'US Population in 1975', geo = map_options) +plot_ly(z = ~state_pop$Pop, text = ~state_pop$hover, + locations = ~state_pop$State, + type = 'choropleth', locationmode = 'USA-states', + color = state_pop$Pop, colors = 'Blues', + marker = list(line = borders)) %>% + layout(title = 'US Population in 1975', + geo = map_options) ``` ## More Resources @@ -291,4 +313,4 @@ plot_ly(z = ~state_pop$Pop, text = ~state_pop$hover, locations = ~state_pop$Stat - [The Plotly R API](https://plot.ly/r/) - [The Plotly R Package on GitHub](https://github.com/ropensci/plotly) - [The Plotly R Cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) -- ["Plotly for R" book by Carson Sievert](https://cpsievert.github.io/plotly_book/) \ No newline at end of file +- ["Plotly for R" book by Carson Sievert](https://cpsievert.github.io/plotly_book/)