christophe dervieux | c4d57b9 | 2021-09-17 10:46:11 +0200 | [diff] [blame] | 1 | --- |
| 2 | title: "Rmarkdown Presentation including Plotly" |
| 3 | output: |
| 4 | revealjs::revealjs_presentation |
| 5 | --- |
| 6 | |
| 7 | # Setup |
| 8 | |
| 9 | ```{r, message=FALSE} |
| 10 | knitr::opts_chunk$set(echo = FALSE) |
| 11 | knitr::opts_template$set(demo = list(echo = TRUE, eval = FALSE)) |
| 12 | xfun::pkg_attach2("highcharter") |
| 13 | xfun::pkg_load2("palmerpenguins") |
| 14 | data(penguins, package = "palmerpenguins") |
| 15 | ``` |
| 16 | |
| 17 | |
| 18 | # Simple example |
| 19 | |
| 20 | ```{r simple} |
| 21 | hchart(penguins, "scatter", hcaes(x = flipper_length_mm, y = bill_length_mm, group = species)) |
| 22 | ``` |
| 23 | |
| 24 | ## Code |
| 25 | |
| 26 | ```{r simple, opts.label = "demo"} |
| 27 | ``` |
| 28 | |
| 29 | # Simple2 example |
| 30 | |
| 31 | ```{r simple2} |
| 32 | x <- c(rnorm(10000), rnorm(1000, 4, 0.5)) |
| 33 | hchart(x, name = "data") |
| 34 | ``` |
| 35 | |
| 36 | ## Code |
| 37 | |
| 38 | ```{r simple2, opts.label = "demo"} |
| 39 | ``` |
| 40 | |
| 41 | # venn |
| 42 | |
| 43 | ```{r venn} |
| 44 | highchart() %>% |
| 45 | hc_chart(type = "venn") %>% |
| 46 | hc_add_series( |
| 47 | dataLabels = list(style = list(fontSize = "20px")), |
| 48 | name = "Venn Diagram", |
| 49 | data = list( |
| 50 | list( |
| 51 | name = "People who are<br>breaking my heart.", |
| 52 | sets = list("A"), value = 5 |
| 53 | ), |
| 54 | list( |
| 55 | name = "People who are shaking<br> my confidence daily.", |
| 56 | sets = list("B"), value = 5 |
| 57 | ), |
| 58 | list( |
| 59 | name = "Cecilia", sets = list("B", "A"), value = 1) |
| 60 | ) |
| 61 | ) |
| 62 | ``` |
| 63 | |
| 64 | ## Code |
| 65 | |
| 66 | ```{r venn, opts.label = "demo"} |
| 67 | ``` |
| 68 | |
| 69 | https://jkunst.com/highcharter/articles/highcharts.html#venn-euler-1 |
| 70 | |
| 71 | # HIMYM example |
| 72 | |
| 73 | ```{r himym, message=FALSE} |
| 74 | data(favorite_bars) |
| 75 | data(favorite_pies) |
| 76 | |
| 77 | highchart() %>% |
| 78 | # Data |
| 79 | hc_add_series( |
| 80 | favorite_pies, |
| 81 | "column", |
| 82 | hcaes( |
| 83 | x = pie, |
| 84 | y = percent |
| 85 | ), |
| 86 | name = "Pie" |
| 87 | ) %>% |
| 88 | hc_add_series( |
| 89 | favorite_bars, |
| 90 | "pie", |
| 91 | hcaes( |
| 92 | name = bar, |
| 93 | y = percent |
| 94 | ), |
| 95 | name = "Bars" |
| 96 | ) %>% |
| 97 | # Options for each type of series |
| 98 | hc_plotOptions( |
| 99 | series = list( |
| 100 | showInLegend = FALSE, |
| 101 | pointFormat = "{point.y}%", |
| 102 | colorByPoint = TRUE |
| 103 | ), |
| 104 | pie = list( |
| 105 | center = c('30%', '10%'), |
| 106 | size = 120, |
| 107 | dataLabels = list(enabled = FALSE) |
| 108 | ) |
| 109 | ) %>% |
| 110 | # Axis |
| 111 | hc_yAxis( |
| 112 | title = list(text = "percentage of tastiness"), |
| 113 | labels = list(format = "{value}%"), |
| 114 | max = 100 |
| 115 | ) %>% |
| 116 | hc_xAxis( |
| 117 | categories = favorite_pies$pie |
| 118 | ) %>% |
| 119 | # Titles, subtitle, caption and credits |
| 120 | hc_title( |
| 121 | text = "How I Met Your Mother: Pie Chart Bar Graph" |
| 122 | ) %>% |
| 123 | hc_subtitle( |
| 124 | text = "This is a bar graph describing my favorite pies |
| 125 | including a pie chart describing my favorite bars" |
| 126 | ) %>% |
| 127 | hc_caption( |
| 128 | text = "The values represented are in percentage of tastiness and awesomeness." |
| 129 | ) %>% |
| 130 | hc_credits( |
| 131 | enabled = TRUE, text = "Source: HIMYM", |
| 132 | href = "https://www.youtube.com/watch?v=f_J8QU1m0Ng", |
| 133 | style = list(fontSize = "12px") |
| 134 | ) %>% |
| 135 | hc_size( |
| 136 | height = 600 |
| 137 | ) |
| 138 | ``` |
| 139 | |
| 140 | ## Code |
| 141 | |
| 142 | https://jkunst.com/highcharter/articles/showcase.html#himym-example-1 |
| 143 | |
| 144 | ```{r himym, opts.label = "demo"} |
| 145 | ``` |
| 146 | |
| 147 | # Stars |
| 148 | |
| 149 | ```{r stars} |
| 150 | data(stars) |
| 151 | |
| 152 | colors <- c( |
| 153 | "#FB1108", "#FD150B", "#FA7806", "#FBE426", "#FCFB8F", |
| 154 | "#F3F5E7", "#C7E4EA", "#ABD6E6", "#9AD2E1" |
| 155 | ) |
| 156 | |
| 157 | stars$color <- highcharter::colorize(log(stars$temp), colors) |
| 158 | |
| 159 | x <- c("Luminosity", "Temperature", "Distance", "Radius") |
| 160 | y <- sprintf("{point.%s:.2f}", c("lum", "temp", "distance", "radiussun")) |
| 161 | |
| 162 | tltip <- tooltip_table(x, y) |
| 163 | |
| 164 | hchart( |
| 165 | stars, |
| 166 | "scatter", |
| 167 | hcaes( |
| 168 | temp, |
| 169 | lum, |
| 170 | size = radiussun, |
| 171 | color = color |
| 172 | ), |
| 173 | minSize = 2, |
| 174 | maxSize = 20 |
| 175 | ) %>% |
| 176 | hc_chart( |
| 177 | # backgroundColor = "black" |
| 178 | backgroundColor = hex_to_rgba("black", 0.5), |
| 179 | divBackgroundImage = "http://www.wired.com/images_blogs/underwire/2013/02/xwing-bg.gif" |
| 180 | ) %>% |
| 181 | hc_xAxis( |
| 182 | title = list(text = "Temperature"), |
| 183 | type = "logarithmic", |
| 184 | gridLineWidth = 0, |
| 185 | reversed = TRUE |
| 186 | ) %>% |
| 187 | hc_yAxis( |
| 188 | title = list(text = "Luminosity"), |
| 189 | type = "logarithmic", |
| 190 | gridLineWidth = 0 |
| 191 | ) %>% |
| 192 | hc_title( |
| 193 | style = list(color = hex_to_rgba("white", 0.5)), |
| 194 | text = "Our nearest Stars" |
| 195 | ) %>% |
| 196 | hc_subtitle( |
| 197 | style = list(color = hex_to_rgba("white", 0.5)), |
| 198 | text = "In a Hertzsprung-Russell diagram" |
| 199 | ) %>% |
| 200 | hc_tooltip( |
| 201 | useHTML = TRUE, |
| 202 | headerFormat = "", |
| 203 | pointFormat = tltip |
| 204 | ) %>% |
| 205 | hc_size( |
| 206 | height = 700 |
| 207 | ) |
| 208 | ``` |
| 209 | |
| 210 | ## Code |
| 211 | |
| 212 | https://jkunst.com/highcharter/articles/showcase.html#himym-example-1 |
| 213 | |
| 214 | ```{r stars, opts.label = "demo"} |
| 215 | ``` |
| 216 | |