Skip to content

Commit b2b6e2a

Browse files
author
Joseph Damiba
committed
adding 3d r charts examples to r-docs repo
1 parent 3a34ee4 commit b2b6e2a

8 files changed

+1092
-0
lines changed

r/2015-07-30-3d-line-plots.Rmd

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
name: 3D Line Plots
3+
permalink: r/3d-line-plots/
4+
description: How to make interactive 3D line plots in R.
5+
layout: base
6+
thumbnail: thumbnail/3d-line.jpg
7+
language: r
8+
page_type: example_index
9+
display_as: 3d_charts
10+
order: 2
11+
output:
12+
html_document:
13+
keep_md: true
14+
---
15+
16+
```{r, echo = FALSE, message=FALSE}
17+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
18+
```
19+
20+
### New to Plotly?
21+
22+
Plotly's R library is free and open source!<br>
23+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
24+
You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br>
25+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
26+
27+
### Version Check
28+
29+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
30+
Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version.
31+
```{r}
32+
library(plotly)
33+
packageVersion('plotly')
34+
```
35+
36+
### Basic 3D Line Plot
37+
38+
```{r}
39+
library(plotly)
40+
41+
data <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/3d-line1.csv')
42+
data$color <- as.factor(data$color)
43+
44+
p <- plot_ly(data, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines',
45+
opacity = 1, line = list(width = 6, color = ~color, reverscale = FALSE))
46+
47+
p
48+
```
49+
50+
### 3D Line and Markers Plot
51+
52+
```{r}
53+
library(plotly)
54+
55+
x <- c()
56+
y <- c()
57+
z <- c()
58+
c <- c()
59+
60+
for (i in 1:62) {
61+
r <- 20 * cos(i / 20)
62+
x <- c(x, r * cos(i))
63+
y <- c(y, r * sin(i))
64+
z <- c(z, i)
65+
c <- c(c, i)
66+
}
67+
68+
data <- data.frame(x, y, z, c)
69+
70+
p <- plot_ly(data, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines+markers',
71+
line = list(width = 6, color = ~c, colorscale = 'Viridis'),
72+
marker = list(size = 3.5, color = ~c, colorscale = 'Greens', cmin = -20, cmax = 50))
73+
74+
p
75+
```
76+
77+
### Custom Color Scale
78+
79+
```{r}
80+
library(plotly)
81+
82+
count <- 3000
83+
84+
x <- c()
85+
y <- c()
86+
z <- c()
87+
c <- c()
88+
89+
for (i in 1:count) {
90+
r <- i * (count - i)
91+
x <- c(x, r * cos(i / 30))
92+
y <- c(y, r * sin(i / 30))
93+
z <- c(z, i)
94+
c <- c(c, i)
95+
}
96+
97+
data <- data.frame(x, y, z, c)
98+
99+
p <- plot_ly(data, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines',
100+
line = list(width = 4, color = ~c, colorscale = list(c(0,'#BA52ED'), c(1,'#FCB040'))))
101+
102+
p
103+
```
104+
105+
### 3D Random Walk Plot
106+
107+
```{r}
108+
library(plotly)
109+
110+
data <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/_3d-line-plot.csv')
111+
112+
p <- plot_ly(data, x = ~x1, y = ~y1, z = ~z1, type = 'scatter3d', mode = 'lines',
113+
line = list(color = '#1f77b4', width = 1)) %>%
114+
add_trace(x = ~x2, y = ~y2, z = ~z2,
115+
line = list(color = 'rgb(44, 160, 44)', width = 1)) %>%
116+
add_trace(x = ~x3, y = ~y3, z = ~z3,
117+
line = list(color = 'bcbd22', width = 1))
118+
119+
p
120+
```
121+
122+
### 3D Density Plot
123+
124+
```{r}
125+
library(plotly)
126+
127+
dens <- with(diamonds, tapply(price, INDEX = cut, density))
128+
data <- data.frame(
129+
x = unlist(lapply(dens, "[[", "x")),
130+
y = unlist(lapply(dens, "[[", "y")),
131+
cut = rep(names(dens), each = length(dens[[1]]$x)))
132+
133+
p <- plot_ly(data, x = ~x, y = ~y, z = ~cut, type = 'scatter3d', mode = 'lines', color = ~cut)
134+
135+
p
136+
```
137+
138+
#Reference
139+
140+
See [https://plot.ly/r/reference/#scatter3d](https://plot.ly/r/reference/#scatter3d) for more information and chart attribute options!

r/2015-07-30-3d-scatter-plots.Rmd

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
name: 3D Scatter Plots
3+
permalink: r/3d-scatter-plots/
4+
description: How to make interactive 3D scatter plots in R.
5+
layout: base
6+
thumbnail: thumbnail/3d-scatter.jpg
7+
language: r
8+
page_type: example_index
9+
display_as: 3d_charts
10+
order: 0
11+
output:
12+
html_document:
13+
keep_md: true
14+
---
15+
16+
```{r, echo = FALSE, message=FALSE}
17+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
18+
```
19+
### New to Plotly?
20+
21+
Plotly's R library is free and open source!<br>
22+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
23+
You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br>
24+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
25+
26+
### Version Check
27+
28+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
29+
Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version.
30+
```{r}
31+
library(plotly)
32+
packageVersion('plotly')
33+
```
34+
35+
#### Basic 3D Scatter Plot
36+
37+
```{r}
38+
library(plotly)
39+
40+
mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
41+
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
42+
mtcars$am <- as.factor(mtcars$am)
43+
44+
p <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~am, colors = c('#BF382A', '#0C4B8E')) %>%
45+
add_markers() %>%
46+
layout(scene = list(xaxis = list(title = 'Weight'),
47+
yaxis = list(title = 'Gross horsepower'),
48+
zaxis = list(title = '1/4 mile time')))
49+
50+
p
51+
```
52+
53+
#### 3D Scatter Plot with Color Scaling
54+
55+
```{r}
56+
library(plotly)
57+
58+
p <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec,
59+
marker = list(color = ~mpg, colorscale = c('#FFE1A1', '#683531'), showscale = TRUE)) %>%
60+
add_markers() %>%
61+
layout(scene = list(xaxis = list(title = 'Weight'),
62+
yaxis = list(title = 'Gross horsepower'),
63+
zaxis = list(title = '1/4 mile time')),
64+
annotations = list(
65+
x = 1.13,
66+
y = 1.05,
67+
text = 'Miles/(US) gallon',
68+
xref = 'paper',
69+
yref = 'paper',
70+
showarrow = FALSE
71+
))
72+
73+
p
74+
```
75+
76+
#### 3D Bubble Plot
77+
78+
```{r}
79+
library(plotly)
80+
81+
data <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
82+
83+
data_2007 <- data[which(data$year == 2007),]
84+
data_2007 <- data_2007[order(data_2007$continent, data_2007$country),]
85+
data_2007$size <- data_2007$pop
86+
colors <- c('#4AC6B7', '#1972A4', '#965F8A', '#FF7070', '#C61951')
87+
88+
p <- plot_ly(data_2007, x = ~gdpPercap, y = ~lifeExp, z = ~pop, color = ~continent, size = ~size, colors = colors,
89+
marker = list(symbol = 'circle', sizemode = 'diameter'), sizes = c(5, 150),
90+
text = ~paste('Country:', country, '<br>Life Expectancy:', lifeExp, '<br>GDP:', gdpPercap,
91+
'<br>Pop.:', pop)) %>%
92+
layout(title = 'Life Expectancy v. Per Capita GDP, 2007',
93+
scene = list(xaxis = list(title = 'GDP per capita (2000 dollars)',
94+
gridcolor = 'rgb(255, 255, 255)',
95+
range = c(2.003297660701705, 5.191505530708712),
96+
type = 'log',
97+
zerolinewidth = 1,
98+
ticklen = 5,
99+
gridwidth = 2),
100+
yaxis = list(title = 'Life Expectancy (years)',
101+
gridcolor = 'rgb(255, 255, 255)',
102+
range = c(36.12621671352166, 91.72921793264332),
103+
zerolinewidth = 1,
104+
ticklen = 5,
105+
gridwith = 2),
106+
zaxis = list(title = 'Population',
107+
gridcolor = 'rgb(255, 255, 255)',
108+
type = 'log',
109+
zerolinewidth = 1,
110+
ticklen = 5,
111+
gridwith = 2)),
112+
paper_bgcolor = 'rgb(243, 243, 243)',
113+
plot_bgcolor = 'rgb(243, 243, 243)')
114+
115+
p
116+
```
117+
118+
#Reference
119+
120+
See [https://plot.ly/r/reference/#scatter3d](https://plot.ly/r/reference/#scatter3d) for more information and chart attribute options!
121+
122+

r/2015-07-30-3d-surface-plots.Rmd

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
name: 3D Surface Plots
3+
permalink: r/3d-surface-plots/
4+
description: How to make interactive 3D surface plots in R.
5+
layout: base
6+
thumbnail: thumbnail/3d-surface.jpg
7+
language: r
8+
page_type: example_index
9+
display_as: 3d_charts
10+
order: 5
11+
output:
12+
html_document:
13+
keep_md: true
14+
---
15+
16+
```{r, echo = FALSE, message=FALSE}
17+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
18+
```
19+
### New to Plotly?
20+
21+
Plotly's R library is free and open source!<br>
22+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
23+
You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br>
24+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
25+
26+
### Version Check
27+
28+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
29+
Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version.
30+
```{r}
31+
library(plotly)
32+
packageVersion('plotly')
33+
```
34+
35+
# Basic 3D Surface Plot
36+
37+
```{r}
38+
library(plotly)
39+
# volcano is a numeric matrix that ships with R
40+
p <- plot_ly(z = ~volcano) %>% add_surface()
41+
42+
p
43+
```
44+
45+
# Surface Plot With Contours
46+
47+
```{r}
48+
library(plotly)
49+
# volcano is a numeric matrix that ships with R
50+
p <- plot_ly(z = ~volcano) %>% add_surface(
51+
contours = list(
52+
z = list(
53+
show=TRUE,
54+
usecolormap=TRUE,
55+
highlightcolor="#ff0000",
56+
project=list(z=TRUE)
57+
)
58+
)
59+
) %>%
60+
layout(
61+
scene = list(
62+
camera=list(
63+
eye = list(x=1.87, y=0.88, z=-0.64)
64+
)
65+
)
66+
)
67+
68+
p
69+
```
70+
71+
### 2D Kernel Density Estimation
72+
73+
```{r}
74+
kd <- with(MASS::geyser, MASS::kde2d(duration, waiting, n = 50))
75+
p <- plot_ly(x = kd$x, y = kd$y, z = kd$z) %>% add_surface()
76+
77+
p
78+
```
79+
80+
### Multiple Surfaces
81+
82+
```{r}
83+
z <- c(
84+
c(8.83,8.89,8.81,8.87,8.9,8.87),
85+
c(8.89,8.94,8.85,8.94,8.96,8.92),
86+
c(8.84,8.9,8.82,8.92,8.93,8.91),
87+
c(8.79,8.85,8.79,8.9,8.94,8.92),
88+
c(8.79,8.88,8.81,8.9,8.95,8.92),
89+
c(8.8,8.82,8.78,8.91,8.94,8.92),
90+
c(8.75,8.78,8.77,8.91,8.95,8.92),
91+
c(8.8,8.8,8.77,8.91,8.95,8.94),
92+
c(8.74,8.81,8.76,8.93,8.98,8.99),
93+
c(8.89,8.99,8.92,9.1,9.13,9.11),
94+
c(8.97,8.97,8.91,9.09,9.11,9.11),
95+
c(9.04,9.08,9.05,9.25,9.28,9.27),
96+
c(9,9.01,9,9.2,9.23,9.2),
97+
c(8.99,8.99,8.98,9.18,9.2,9.19),
98+
c(8.93,8.97,8.97,9.18,9.2,9.18)
99+
)
100+
dim(z) <- c(15,6)
101+
z2 <- z + 1
102+
z3 <- z - 1
103+
104+
p <- plot_ly(showscale = FALSE) %>%
105+
add_surface(z = ~z) %>%
106+
add_surface(z = ~z2, opacity = 0.98) %>%
107+
add_surface(z = ~z3, opacity = 0.98)
108+
109+
p
110+
```

0 commit comments

Comments
 (0)