Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hkmoon/5635b995c3b6ee4417f124ad2c7b010c to your computer and use it in GitHub Desktop.
Save hkmoon/5635b995c3b6ee4417f124ad2c7b010c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"library(repr)\noptions(repr.plot.width=7, repr.plot.height=7, repr.plot.res = 200)\noptions(warn=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(ggplot2)\n\n# https://plotly-r.com/\n\np <- qplot(wt, mpg, data=mtcars, geom=c('point', 'smooth'),\n method='lm', formular=y~x, color=cyl,\n main='Regression of MPG on Weight',\n xlab='Weight', ylab='Miles per Gallon')\n\np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(plotly)\n\nembed_notebook(ggplotly(p), height=\"500\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# install.packages(\"babynames\", repos=\"http://cran.r-project.org\")\n\nlibrary(babynames)\nnms <- filter(babynames, name %in% c(\"Sam\", \"Jamie\"))\np <- ggplot(nms) + \n geom_line(aes(year, prop, color = sex, linetype = name))\n \nembed_notebook(ggplotly(p, dynamicTicks = TRUE) %>%\n rangeslider() %>%\n layout(hovermode = \"x\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p <- ggplot(mtcars, aes(x = wt, y = mpg)) +\n geom_point() + geom_smooth()\nembed_notebook(ggplotly(p))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mtcars\np %>%\n ggplotly(layerData = 2, originalData = FALSE) %>%\n plotly_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p %>%\n ggplotly(layerData = 2, originalData = FALSE) %>%\n add_fun(function(p) {\n p %>% slice(which.max(se)) %>%\n add_segments(x = ~x, xend = ~x, y = ~ymin, yend = ~ymax) %>%\n add_annotations(\"Maximum uncertainty\", ax = 60)\n }) %>%\n add_fun(function(p) {\n p %>% slice(which.min(se)) %>%\n add_segments(x = ~x, xend = ~x, y = ~ymin, yend = ~ymax) %>%\n add_annotations(\"Minimum uncertainty\")\n }) %>% embed_notebook\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d <- highlight_key(mpg)\nbase <- plot_ly(d, color = I(\"black\"), showlegend = FALSE)\n\nsubplot(\n add_markers(base, x = ~displ, y = ~hwy),\n add_boxplot(base, x = ~fl, y = ~hwy) %>%\n add_markers(x = ~fl, y = ~hwy, alpha = 0.1),\n add_trace(base, x = ~drv, y = ~hwy, type = \"violin\") %>%\n add_markers(x = ~drv, y = ~hwy, alpha = 0.1),\n shareY = TRUE\n) %>%\n subplot(add_histogram(base, x = ~class), nrows = 2) %>%\n # Selections are actually additional traces, and, by default, \n # plotly.js will try to dodge bars placed under the same category\n layout(barmode = \"overlay\") %>%\n highlight(\"plotly_selected\", dynamic = TRUE) %>%\n embed_notebook","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"m <- highlight_key(mpg)\np <- ggplot(m, aes(displ, hwy, colour = class)) +\n geom_point() +\n geom_smooth(se = FALSE, method = \"lm\")\nggplotly(p) %>% highlight(\"plotly_hover\") %>% embed_notebook","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"m <- highlight_key(mpg, ~class)\np1 <- ggplot(m, aes(displ, fill = class)) + geom_density()\np2 <- ggplot(m, aes(displ, hwy, fill = class)) + geom_point()\n\nsubplot(p1, p2) %>% hide_legend() %>% highlight(\"plotly_hover\") %>% embed_notebook","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# install.packages(\"GGally\", repos=\"http://cran.r-project.org\")\nlibrary(GGally)\n\nhighlight_key(iris) %>%\n GGally::ggpairs(aes(color = Species), columns = 1:4) %>%\n ggplotly() %>%\n\nhighlight(\"plotly_selected\") %>% embed_notebook(height=\"600\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(dplyr)\nmtcars %>%\n # for better tick labels\n mutate(am = recode(am, `0` = \"automatic\", `1` = \"manual\")) %>%\n lm(mpg ~ wt + qsec + am, data = .) %>%\n ggnostic(mapping = aes(color = am)) %>%\n ggplotly() %>% embed_notebook(height=\"600\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# install.packages(\"USArrests\", repos=\"http://cran.r-project.org\")\n# install.packages(\"dendextend\", repos=\"http://cran.r-project.org\")\n\nhc <- hclust(dist(USArrests), \"ave\")\ndend1 <- as.dendrogram(hc)\nplot_dendro(dend1, height = 800) %>% \n hide_legend() %>% \n highlight(\"plotly_selected\", persistent = TRUE, dynamic = TRUE) %>%\n embed_notebook(height=\"900\", width=\"500\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(heatmaply)\n\nmtscaled <- as.matrix(scale(mtcars))\nheatmaply(mtscaled, k_row = 3, k_col = 2) %>% embed_notebook(height=\"800\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"hide_input":false,"toc":{"nav_menu":{},"number_sections":false,"sideBar":false,"skip_h1_title":false,"base_numbering":1,"title_cell":"Table of Contents","title_sidebar":"Contents","toc_cell":false,"toc_position":{},"toc_section_display":false,"toc_window_display":false},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"3.6.2"},"celltoolbar":"Raw Cell Format"},"nbformat":4,"nbformat_minor":2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment