|
| 1 | +--- |
| 2 | +title: 3D Surface Lighting in R | Examples | Plotly |
| 3 | +name: 3D Surface Lighting in R |
| 4 | +permalink: r/3d-surface-lighting/ |
| 5 | +description: How to add lighting effects in 3D R Plots. |
| 6 | +layout: base |
| 7 | +thumbnail: thumbnail/your-tutorial-chart.jpg |
| 8 | +language: r |
| 9 | +page_type: example_index |
| 10 | +has_thumbnail: true |
| 11 | +display_as: layout_opt |
| 12 | +output: |
| 13 | + html_document: |
| 14 | + keep_md: true |
| 15 | +--- |
| 16 | + |
| 17 | +```{r, echo = FALSE, message=FALSE} |
| 18 | +knitr::opts_chunk$set(message = FALSE, warning=FALSE) |
| 19 | +Sys.setenv("plotly_username"="RPlotBot") |
| 20 | +Sys.setenv("plotly_api_key"="q0lz6r5efr") |
| 21 | +``` |
| 22 | +### New to Plotly? |
| 23 | + |
| 24 | +Plotly's R library is free and open source!<br> |
| 25 | +[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br> |
| 26 | +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> |
| 27 | +We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! |
| 28 | + |
| 29 | +### Version Check |
| 30 | + |
| 31 | +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br> |
| 32 | +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. |
| 33 | +```{r} |
| 34 | +library(plotly) |
| 35 | +packageVersion('plotly') |
| 36 | +``` |
| 37 | + |
| 38 | +### Ambient |
| 39 | + |
| 40 | +```{r, results = 'hide'} |
| 41 | +p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(ambient = 0.2)) %>% |
| 42 | + add_surface(showscale=FALSE) |
| 43 | +
|
| 44 | +p2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(ambient = 0.9)) %>% |
| 45 | + add_surface(showscale=FALSE) |
| 46 | +
|
| 47 | +p <- subplot(p1, p2) %>% |
| 48 | + layout(title = "Ambient Lighting", |
| 49 | + grid = list(rows = 1, columns = 2, |
| 50 | + pattern = 'independent'), |
| 51 | + scene = list(domain=list(column = 0), |
| 52 | + aspectmode='cube'), |
| 53 | + scene2 = list(domain=list(column = 1), |
| 54 | + aspectmode='cube')) |
| 55 | +
|
| 56 | +# Create a shareable link to your chart |
| 57 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 58 | +chart_link = api_create(p, filename="3d-surface-ambient") |
| 59 | +chart_link |
| 60 | +``` |
| 61 | + |
| 62 | +```{r, echo=FALSE} |
| 63 | +chart_link |
| 64 | +``` |
| 65 | + |
| 66 | +### Roughness |
| 67 | + |
| 68 | +```{r, results = 'hide'} |
| 69 | +p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(roughness = 0.1)) %>% |
| 70 | + add_surface(showscale=FALSE) |
| 71 | +
|
| 72 | +p2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(roughness = 0.9)) %>% |
| 73 | + add_surface(showscale=FALSE) |
| 74 | +
|
| 75 | +p <- subplot(p1, p2) %>% |
| 76 | + layout(title = "Roughness", |
| 77 | + grid = list(rows = 1, columns = 2, |
| 78 | + pattern = 'independent'), |
| 79 | + scene = list(domain=list(column = 0), |
| 80 | + aspectmode='cube'), |
| 81 | + scene2 = list(domain=list(column = 1), |
| 82 | + aspectmode='cube')) |
| 83 | +
|
| 84 | +# Create a shareable link to your chart |
| 85 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 86 | +chart_link = api_create(p, filename="3d-surface-roughness") |
| 87 | +chart_link |
| 88 | +``` |
| 89 | + |
| 90 | +```{r, echo=FALSE} |
| 91 | +chart_link |
| 92 | +``` |
| 93 | + |
| 94 | +### Diffuse |
| 95 | + |
| 96 | +```{r, results = 'hide'} |
| 97 | +p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(diffuse = 0.1)) %>% |
| 98 | + add_surface(showscale=FALSE) |
| 99 | +
|
| 100 | +p2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(diffuse = 0.9)) %>% |
| 101 | + add_surface(showscale=FALSE) |
| 102 | +
|
| 103 | +p <- subplot(p1, p2) %>% |
| 104 | + layout(title = "Diffuse Reflection", |
| 105 | + grid = list(rows = 1, columns = 2, |
| 106 | + pattern = 'independent'), |
| 107 | + scene = list(domain=list(column = 0), |
| 108 | + aspectmode='cube'), |
| 109 | + scene2 = list(domain=list(column = 1), |
| 110 | + aspectmode='cube')) |
| 111 | +
|
| 112 | +# Create a shareable link to your chart |
| 113 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 114 | +chart_link = api_create(p, filename="3d-surface-diffuse") |
| 115 | +chart_link |
| 116 | +``` |
| 117 | + |
| 118 | +```{r, echo=FALSE} |
| 119 | +chart_link |
| 120 | +``` |
| 121 | + |
| 122 | +### Specular |
| 123 | + |
| 124 | +```{r, results = 'hide'} |
| 125 | +p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(specular = 0.1)) %>% |
| 126 | + add_surface(showscale=FALSE) |
| 127 | +
|
| 128 | +p2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(specular = 1.9)) %>% |
| 129 | + add_surface(showscale=FALSE) |
| 130 | +
|
| 131 | +p <- subplot(p1, p2) %>% |
| 132 | + layout(title = "Specular Reflection", |
| 133 | + grid = list(rows = 1, columns = 2, |
| 134 | + pattern = 'independent'), |
| 135 | + scene = list(domain=list(column = 0), |
| 136 | + aspectmode='cube'), |
| 137 | + scene2 = list(domain=list(column = 1), |
| 138 | + aspectmode='cube')) |
| 139 | +
|
| 140 | +# Create a shareable link to your chart |
| 141 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 142 | +chart_link = api_create(p, filename="3d-surface-specular") |
| 143 | +chart_link |
| 144 | +``` |
| 145 | + |
| 146 | +```{r, echo=FALSE} |
| 147 | +chart_link |
| 148 | +``` |
| 149 | + |
| 150 | +### Fresnel |
| 151 | + |
| 152 | +```{r, results = 'hide'} |
| 153 | +p1 <- plot_ly(z = ~volcano, scene='scene1', lighting = list(fresnel = 0.1)) %>% |
| 154 | + add_surface(showscale=FALSE) |
| 155 | +
|
| 156 | +p2 <- plot_ly(z = ~volcano, scene='scene2', lighting = list(fresnel = 4.5)) %>% |
| 157 | + add_surface(showscale=FALSE) |
| 158 | +
|
| 159 | +p <- subplot(p1, p2) %>% |
| 160 | + layout(title = "Fresnel", |
| 161 | + grid = list(rows = 1, columns = 2, |
| 162 | + pattern = 'independent'), |
| 163 | + scene = list(domain=list(column = 0), |
| 164 | + aspectmode='cube'), |
| 165 | + scene2 = list(domain=list(column = 1), |
| 166 | + aspectmode='cube')) |
| 167 | +
|
| 168 | +# Create a shareable link to your chart |
| 169 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 170 | +chart_link = api_create(p, filename="3d-surface-fresnel") |
| 171 | +chart_link |
| 172 | +``` |
| 173 | + |
| 174 | +```{r, echo=FALSE} |
| 175 | +chart_link |
| 176 | +``` |
| 177 | + |
| 178 | +#Reference |
| 179 | + |
| 180 | +See [https://plot.ly/r/reference/#surface-lighting](https://plot.ly/r/reference/#surface-lighting) for more information and options! |
| 181 | + |
0 commit comments