Assignment: Create a web page presentation using R Markdown that features interactive plots created with Plotly.
This presentation features a comparison of interactive maps created using Plotly: 3D surface, line plots & contour. Data used was shared from the web. Parameters used were derived for different effects. Each plot consists of variations in line, direction and surface. This is meant to be a brief intro to Plotly features and capabilities.
library(ggplot2)
library(plotly)
##
## 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
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
data("presidents")
data("volcano")
names(volcano)
## NULL
dim(volcano)
## [1] 87 61
str(volcano)
## num [1:87, 1:61] 100 101 102 103 104 105 105 106 107 108 ...
head(volcano)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
## [1,] 100 100 101 101 101 101 101 100 100 100 101 101 102 102
## [2,] 101 101 102 102 102 102 102 101 101 101 102 102 103 103
## [3,] 102 102 103 103 103 103 103 102 102 102 103 103 104 104
## [4,] 103 103 104 104 104 104 104 103 103 103 103 104 104 104
## [5,] 104 104 105 105 105 105 105 104 104 103 104 104 105 105
## [6,] 105 105 105 106 106 106 106 105 105 104 104 105 105 106
## [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
## [1,] 102 102 103 104 103 102 101 101 102 103 104 104
## [2,] 103 103 104 105 104 103 102 102 103 105 106 106
## [3,] 104 104 105 106 105 104 104 105 106 107 108 110
## [4,] 105 105 106 107 106 106 106 107 108 110 111 114
## [5,] 105 106 107 108 108 108 109 110 112 114 115 118
## [6,] 106 107 109 110 110 112 113 115 116 118 119 121
## [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
## [1,] 105 107 107 107 108 108 110 110 110 110 110 110
## [2,] 107 109 110 110 110 110 111 112 113 114 116 115
## [3,] 111 113 114 115 114 115 116 118 119 119 121 121
## [4,] 117 118 117 119 120 121 122 124 125 126 127 127
## [5,] 121 122 121 123 128 131 129 130 131 131 132 132
## [6,] 124 126 126 129 134 137 137 136 136 135 136 136
## [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
## [1,] 110 110 108 108 108 107 107 108 108 108 108 108
## [2,] 114 112 110 110 110 109 108 109 109 109 109 108
## [3,] 120 118 116 114 112 111 110 110 110 110 109 109
## [4,] 126 124 122 120 117 116 113 111 110 110 110 109
## [5,] 131 130 128 126 122 119 115 114 112 110 110 110
## [6,] 136 135 133 129 126 122 118 116 115 113 111 110
## [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61]
## [1,] 107 107 107 107 106 106 105 105 104 104 103
## [2,] 108 108 108 107 107 106 106 105 105 104 104
## [3,] 109 109 108 108 107 107 106 106 105 105 104
## [4,] 109 109 109 108 108 107 107 106 106 105 105
## [5,] 110 110 109 109 108 107 107 107 106 106 105
## [6,] 110 110 110 109 108 108 108 107 107 106 106
### 1 Surface
# aplot: 'surface plot in 3D- single surface'
aplot <- plot_ly(z=volcano, type="surface")
aplot
### 2-scatter3D
# bplot: 'scatter3D plot in lines mode'
# get csv of 3d-line data for 3d line plots
data <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/3d-line1.csv')
names(data)
## [1] "x" "y" "z" "color"
dim(data)
## [1] 601 4
str(data)
## 'data.frame': 601 obs. of 4 variables:
## $ x : num 100 99.2 99.6 97.9 96.8 ...
## $ y : num 0.613 0.59 0.6 0.549 0.516 ...
## $ z : num 0.735 0.781 0.763 0.86 0.927 ...
## $ color: int 0 0 0 0 0 0 0 0 1 0 ...
head(data)
## x y z color
## 1 100.00000 0.6132220 0.7347065 0
## 2 99.23888 0.5898520 0.7813203 0
## 3 99.55961 0.5997426 0.7625658 0
## 4 97.93142 0.5492963 0.8599657 0
## 5 96.83783 0.5156126 0.9271499 0
## 6 98.54580 0.5688320 0.8263298 0
data$color <- as.factor(data$color)
bplot <- plot_ly(data, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'line',
opacity = 7,
line = list(width = 6, color = ~color, colorscale = 'Viridis', reverscale = FALSE)
)
bplot
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
### 3-scatter3D
# dplot: 'scatter3D plot in lines and markers mode'
# use a 4D data frame
x <- c()
y <- c()
z <- c()
c <- c()
for (i in 1:60) {
r <- 20 * cos(i / 30)
x <- c(x, r * cos(i))
y <- c(y, r * sin(i))
z <- c(z, i)
c <- c(c, i)
}
data <- data.frame(x, y, z, c)
names(data)
## [1] "x" "y" "z" "c"
dim(data)
## [1] 60 4
str(data)
## 'data.frame': 60 obs. of 4 variables:
## $ x: num 10.8 -8.3 -19.7 -12.96 5.59 ...
## $ y: num 16.82 18.15 2.81 -15 -18.91 ...
## $ z: int 1 2 3 4 5 6 7 8 9 10 ...
## $ c: int 1 2 3 4 5 6 7 8 9 10 ...
head(data)
## x y z c
## 1 10.800043 16.820071 1 1
## 2 -8.304448 18.145550 2 2
## 3 -19.700933 2.808300 3 3
## 4 -12.956841 -15.001706 4 4
## 5 5.594631 -18.912734 5 5
## 6 18.820616 -5.476916 6 6
dplot <- plot_ly(data, x = ~x, y = ~y, z = ~z, type = 'scatter3d', mode = 'lines+markers',
line = list(width = 5, color = ~c, colorscale = 'Viridis'),
marker = list(size = 4.5, color = ~c, colorscale = 'Greens', cmin = -20, cmax = 100))
dplot
### 4-multiple surface
# dplot: 'surface plot in 3D with multiple surfaces'
# create custom data frame
z <- c(
c(8.83,8.89,8.81,8.87,8.9,8.87),
c(8.89,8.94,8.85,8.94,8.96,8.92),
c(8.84,8.9,8.82,8.92,8.93,8.91),
c(8.79,8.85,8.79,8.9,8.94,8.92),
c(8.79,8.88,8.81,8.9,8.95,8.92),
c(8.8,8.82,8.78,8.91,8.94,8.92),
c(8.75,8.78,8.77,8.91,8.95,8.92),
c(8.8,8.8,8.77,8.91,8.95,8.94),
c(8.74,8.81,8.76,8.93,8.98,8.99),
c(8.89,8.99,8.92,9.1,9.13,9.11),
c(8.97,8.97,8.91,9.09,9.11,9.11),
c(9.04,9.08,9.05,9.25,9.28,9.27),
c(9,9.01,9,9.2,9.23,9.2),
c(8.99,8.99,8.98,9.18,9.2,9.19),
c(8.93,8.97,8.97,9.18,9.2,9.18)
)
names(z)
## NULL
dim(z)
## NULL
str(z)
## num [1:90] 8.83 8.89 8.81 8.87 8.9 8.87 8.89 8.94 8.85 8.94 ...
head(z)
## [1] 8.83 8.89 8.81 8.87 8.90 8.87
dim(z) <- c(15,6)
z2 <- z + .79
z3 <- z - .59
#c <- plot_ly(type = 'scatter3d')
c <- plot_ly(type = 'surface', colorscale = 'Viridis')
c <- c %>% add_surface(z = ~z, opacity = 0.95)
c <- c %>% add_surface(z = ~z2, opacity = 0.90)
c <- c %>% add_surface(z = ~z3, opacity = 0.85)
dplot <- c
dplot
### 5- contour with elevation in meters
# eplot: 'contour plot'
# use volcano data shared on the web
names(volcano)
## NULL
dim(volcano)
## [1] 87 61
str(volcano)
## num [1:87, 1:61] 100 101 102 103 104 105 105 106 107 108 ...
head(volcano)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
## [1,] 100 100 101 101 101 101 101 100 100 100 101 101 102 102
## [2,] 101 101 102 102 102 102 102 101 101 101 102 102 103 103
## [3,] 102 102 103 103 103 103 103 102 102 102 103 103 104 104
## [4,] 103 103 104 104 104 104 104 103 103 103 103 104 104 104
## [5,] 104 104 105 105 105 105 105 104 104 103 104 104 105 105
## [6,] 105 105 105 106 106 106 106 105 105 104 104 105 105 106
## [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
## [1,] 102 102 103 104 103 102 101 101 102 103 104 104
## [2,] 103 103 104 105 104 103 102 102 103 105 106 106
## [3,] 104 104 105 106 105 104 104 105 106 107 108 110
## [4,] 105 105 106 107 106 106 106 107 108 110 111 114
## [5,] 105 106 107 108 108 108 109 110 112 114 115 118
## [6,] 106 107 109 110 110 112 113 115 116 118 119 121
## [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
## [1,] 105 107 107 107 108 108 110 110 110 110 110 110
## [2,] 107 109 110 110 110 110 111 112 113 114 116 115
## [3,] 111 113 114 115 114 115 116 118 119 119 121 121
## [4,] 117 118 117 119 120 121 122 124 125 126 127 127
## [5,] 121 122 121 123 128 131 129 130 131 131 132 132
## [6,] 124 126 126 129 134 137 137 136 136 135 136 136
## [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
## [1,] 110 110 108 108 108 107 107 108 108 108 108 108
## [2,] 114 112 110 110 110 109 108 109 109 109 109 108
## [3,] 120 118 116 114 112 111 110 110 110 110 109 109
## [4,] 126 124 122 120 117 116 113 111 110 110 110 109
## [5,] 131 130 128 126 122 119 115 114 112 110 110 110
## [6,] 136 135 133 129 126 122 118 116 115 113 111 110
## [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61]
## [1,] 107 107 107 107 106 106 105 105 104 104 103
## [2,] 108 108 108 107 107 106 106 105 105 104 104
## [3,] 109 109 108 108 107 107 106 106 105 105 104
## [4,] 109 109 109 108 108 107 107 106 106 105 105
## [5,] 110 110 109 109 108 107 107 107 106 106 105
## [6,] 110 110 110 109 108 108 108 107 107 106 106
eplot <- plot_ly(z = volcano, type = "contour", contours = list(showlabels = TRUE))
eplot <- eplot %>% colorbar(title = "Elevation \n in meters")
eplot
#plot_ly(plotly) #initiate a plotly visualization
# This function maps R objects to plotly.js, an (MIT licensed) web-based interactive charting library. It provides abstractions for doing common things (e.g. mapping data values to fill colors (via color) or creating animations (via frame)) and sets some different defaults to make the interface feel more 'R-like' (i.e., closer to plot() and ggplot2::qplot()).
# usage: plot_ly(data.frame(), ... type = NULL)
# dataframe: (optional)
#type: A character string specifying the trace type (e.g. "scatter", "bar", "box", etc). If specified, it always creates a trace, otherwise
Note that the echo = FALSE parameter may be added to a code chunk to prevent printing of the R code that generated the plot.