Skip to content

Commit dad07d8

Browse files
authored
Merge pull request #348 from UBC-DSCI/patch-plotly-pdf
Patch plotly pdf
2 parents 01fa67d + b5ba852 commit dad07d8

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

build_html.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Script to generate HTML book
2-
docker run --rm -m 5g -v $(pwd):/home/rstudio/introduction-to-datascience ubcdsci/intro-to-ds:v0.15.0 /bin/bash -c "cd /home/rstudio/introduction-to-datascience; Rscript _build_html.r"
2+
docker run --rm -m 5g -v $(pwd):/home/rstudio/introduction-to-datascience ubcdsci/intro-to-ds:v0.16.0 /bin/bash -c "cd /home/rstudio/introduction-to-datascience; Rscript _build_html.r"

build_pdf.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ cp -r data/ pdf/data
2222
cp -r img/ pdf/img
2323

2424
# Build the book with bookdown
25-
docker run --rm -m 5g -v $(pwd):/home/rstudio/introduction-to-datascience ubcdsci/intro-to-ds:v0.15.0 /bin/bash -c "cd /home/rstudio/introduction-to-datascience/pdf; Rscript _build_pdf.r"
25+
docker run --rm -m 5g -v $(pwd):/home/rstudio/introduction-to-datascience ubcdsci/intro-to-ds:v0.16.0 /bin/bash -c "cd /home/rstudio/introduction-to-datascience/pdf; Rscript _build_pdf.r"
2626

2727
# clean files in pdf dir
2828
rm -rf pdf/references.bib

classification1.Rmd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ knitr::opts_chunk$set(echo = TRUE,
1010
options(knitr.table.format = function() {
1111
if (knitr::is_latex_output()) 'latex' else 'pandoc'
1212
})
13+
reticulate::use_miniconda('r-reticulate')
1314
```
1415

1516
## Overview
@@ -568,7 +569,7 @@ Based on $K=5$ nearest neighbors with these three predictors we would classify t
568569
Figure \@ref(fig:05-more) shows what the data look like when we visualize them
569570
as a 3-dimensional scatter with lines from the new observation to its five nearest neighbors.
570571

571-
```{r 05-more, echo = FALSE, message = FALSE, fig.cap = "3D scatter plot of the standardized symmetry, concavity, and perimeter variables. Note that in general we recommend against using 3D visualizations; here we show the data in 3D only to illustrate what higher dimensions and nearest neighbors look like, for learning purposes.", fig.retina=2, out.width="80%"}
572+
```{r 05-more, echo = FALSE, message = FALSE, fig.cap = "3D scatter plot of the standardized symmetry, concavity, and perimeter variables. Note that in general we recommend against using 3D visualizations; here we show the data in 3D only to illustrate what higher dimensions and nearest neighbors look like, for learning purposes.", fig.retina=2, out.width="100%"}
572573
attrs <- c("Perimeter", "Concavity", "Symmetry")
573574
574575
# create new scaled obs and get NNs
@@ -598,7 +599,7 @@ plot_3d <- scaled_cancer_3 |>
598599
z = ~Symmetry,
599600
color = ~Class,
600601
opacity = 0.4,
601-
size = 150,
602+
size = 2,
602603
colors = c("orange2", "steelblue2", "red"),
603604
symbol = ~Class, symbols = c('circle','circle','diamond'))
604605
@@ -637,6 +638,11 @@ plot_3d <- plot_3d %>%
637638
if(!is_latex_output()){
638639
plot_3d
639640
} else {
641+
# scene = list(camera = list(eye = list(x=2, y=2, z = 1.5)))
642+
# plot_3d <- plot_3d %>% layout(scene = scene)
643+
# save_image(plot_3d, "img/plot3d_knn_classification.png", scale = 10)
644+
# cannot adjust size of points in this plot for pdf
645+
# so using a screenshot for now instead
640646
knitr::include_graphics("img/plot3d_knn_classification.png")
641647
}
642648
```

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
book-env:
3-
image: ubcdsci/intro-to-ds:v0.12.0
3+
image: ubcdsci/intro-to-ds:v0.16.0
44
ports:
55
- "8787:8787"
66
volumes:
@@ -10,5 +10,5 @@ services:
1010
deploy:
1111
resources:
1212
limits:
13-
memory: 4G
13+
memory: 5G
1414

regression1.Rmd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ library(knitr)
55
library(plotly)
66
77
knitr::opts_chunk$set(fig.align = "center")
8+
reticulate::use_miniconda('r-reticulate')
89
```
910

1011
## Overview
@@ -759,7 +760,7 @@ Figure \@ref(fig:07-knn-mult-viz) visualizes the model's predictions overlaid on
759760
time the predictions are a surface in 3D space, instead of a line in 2D space, as we have 2
760761
predictors instead of 1.
761762

762-
```{r 07-knn-mult-viz, echo = FALSE, message = FALSE, warning = FALSE, fig.cap = "KNN regression model’s predictions represented as a surface in 3D space overlaid on top of the data using three predictors (price, house size, and the number of bedrooms). Note that in general we recommend against using 3D visualizations; here we use a 3D visualization only to illustrate what the surface of predictions looks like for learning purposes.", out.width="80%"}
763+
```{r 07-knn-mult-viz, echo = FALSE, message = FALSE, warning = FALSE, fig.cap = "KNN regression model’s predictions represented as a surface in 3D space overlaid on top of the data using three predictors (price, house size, and the number of bedrooms). Note that in general we recommend against using 3D visualizations; here we use a 3D visualization only to illustrate what the surface of predictions looks like for learning purposes.", out.width="100%"}
763764
xvals <- seq(from = min(sacramento_train$sqft),
764765
to = max(sacramento_train$sqft),
765766
length = 50)
@@ -780,12 +781,12 @@ plot_3d <- plot_ly() |>
780781
x = ~sqft,
781782
y = ~beds,
782783
z = ~price,
783-
marker = list(size = 5, opacity = 0.4, color = "red")
784+
marker = list(size = 2, opacity = 0.4, color = "red")
784785
) |>
785786
layout(scene = list(
786-
xaxis = list(title = "House size (square feet)"),
787+
xaxis = list(title = "Size (sq ft)"),
787788
zaxis = list(title = "Price (USD)"),
788-
yaxis = list(title = "Number of bedrooms")
789+
yaxis = list(title = "Bedrooms")
789790
)) |>
790791
add_surface(
791792
x = ~xvals,
@@ -797,6 +798,9 @@ plot_3d <- plot_ly() |>
797798
if(!is_latex_output()){
798799
plot_3d
799800
} else {
801+
scene = list(camera = list(eye = list(x = -2.1, y = -2.2, z = 0.75)))
802+
plot_3d <- plot_3d |> layout(scene = scene)
803+
save_image(plot_3d, "img/plot3d_knn_regression.png", scale = 10)
800804
knitr::include_graphics("img/plot3d_knn_regression.png")
801805
}
802806
```

regression2.Rmd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ library(knitr)
55
library(plotly)
66
77
knitr::opts_chunk$set(fig.align = "center")
8+
reticulate::use_miniconda('r-reticulate')
89
```
910

1011
## Overview
@@ -453,7 +454,7 @@ is `r format(round(lm_mult_test_results %>% filter(.metric == 'rmse') %>% pull(.
453454
In the case of two predictors, we can plot the predictions made by our linear regression creates a *plane* of best fit, as
454455
shown in Figure \@ref(fig:08-3DlinReg).
455456

456-
```{r 08-3DlinReg, echo = FALSE, message = FALSE, warning = FALSE, fig.cap = "Linear regression plane of best fit overlaid on top of the data (using price, house size, and number of bedrooms as predictors). Note that in general we recommend against using 3D visualizations; here we use a 3D visualization only to illustrate what the regression plane looks like for learning purposes.", out.width="80%"}
457+
```{r 08-3DlinReg, echo = FALSE, message = FALSE, warning = FALSE, fig.cap = "Linear regression plane of best fit overlaid on top of the data (using price, house size, and number of bedrooms as predictors). Note that in general we recommend against using 3D visualizations; here we use a 3D visualization only to illustrate what the regression plane looks like for learning purposes.", out.width="100%"}
457458
xvals <- seq(from = min(sacramento_train$sqft),
458459
to = max(sacramento_train$sqft),
459460
length = 50)
@@ -474,12 +475,12 @@ plot_3d <- plot_ly() |>
474475
x = ~sqft,
475476
y = ~beds,
476477
z = ~price,
477-
marker = list(size = 5, opacity = 0.4, color = "red")
478+
marker = list(size = 2, opacity = 0.4, color = "red")
478479
) |>
479480
layout(scene = list(
480-
xaxis = list(title = "House size (square feet)"),
481+
xaxis = list(title = "Size (sq ft)"),
481482
zaxis = list(title = "Price (USD)"),
482-
yaxis = list(title = "Number of bedrooms")
483+
yaxis = list(title = "Bedrooms")
483484
)) |>
484485
add_surface(
485486
x = ~xvals,
@@ -491,6 +492,9 @@ plot_3d <- plot_ly() |>
491492
if(!is_latex_output()){
492493
plot_3d
493494
} else {
495+
scene = list(camera = list(eye = list(x = -2.1, y = -2.2, z = 0.75)))
496+
plot_3d <- plot_3d %>% layout(scene = scene)
497+
save_image(plot_3d, "img/plot3d_linear_regression.png", scale = 10)
494498
knitr::include_graphics("img/plot3d_linear_regression.png")
495499
}
496500
```

0 commit comments

Comments
 (0)