Skip to content

Wrap all code blocks to at most 57 columns in plotly.Rmd #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions plotly/plotly.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,31 @@ 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

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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
```

Expand All @@ -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, '<br>', "Population:", Pop))
state_pop$hover <- with(state_pop, paste(State, '<br>',
"Population:", Pop))
# Make state borders white
borders <- list(color = toRGB("red"))
# Set up some mapping options
Expand All @@ -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, '<br>', "Population:", Pop))
state_pop$hover <- with(state_pop,
paste(State, '<br>', "Population:", Pop))
# Make state borders white
borders <- list(color = toRGB("red"))
# Set up some mapping options
Expand All @@ -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
Expand All @@ -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/)
- ["Plotly for R" book by Carson Sievert](https://cpsievert.github.io/plotly_book/)