{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Scatter plot" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Scatter plot\n", "library(plotly)\n", "\n", "set.seed(123)\n", "\n", "x <- rnorm(1000)\n", "y <- rchisq(1000, df = 1, ncp = 0)\n", "color <- sample(LETTERS[1:5], size = 1000, replace = T)\n", "size <- sample(1:5, size = 1000, replace = T)\n", "\n", "ds <- data.frame(x, y, color, size)\n", "\n", "p <- plot_ly(ds, x = ~x, y = ~y, color = ~color, size = ~size) %>% \n", " layout(title = \"Scatter plot in\")\n", "embed_notebook(p)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filled line chart\n", "Apart from plots and figures, *tables* and *text output* can shown as well. Just like in *R-Markdown*.\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
HAM1HAM2HAM3HAM4HAM5HAM6EDHEC LS EQSP500 TRUS 10Y TRUS 3m TRDates
1996-01-310.0074NA0.03490.0222NANANA0.0340.00380.004561996-01-31
1996-02-290.0193NA0.03510.0195NANANA0.0093-0.035320.003981996-02-29
1996-03-310.0155NA0.0258-0.0098NANANA0.0096-0.010570.003711996-03-31
1996-04-30-0.0091NA0.04490.0236NANANA0.0147-0.017390.004281996-04-30
1996-05-310.0076NA0.03530.0028NANANA0.0258-0.005430.004431996-05-31
1996-06-30-0.0039NA-0.0303-0.0019NANANA0.00380.015070.004121996-06-30
\n" ], "text/latex": [ "\\begin{tabular}{r|lllllllllll}\n", " & HAM1 & HAM2 & HAM3 & HAM4 & HAM5 & HAM6 & EDHEC LS EQ & SP500 TR & US 10Y TR & US 3m TR & Dates\\\\\n", "\\hline\n", "\t1996-01-31 & 0.0074 & NA & 0.0349 & 0.0222 & NA & NA & NA & 0.034 & 0.0038 & 0.00456 & 1996-01-31\\\\\n", "\t1996-02-29 & 0.0193 & NA & 0.0351 & 0.0195 & NA & NA & NA & 0.0093 & -0.03532 & 0.00398 & 1996-02-29\\\\\n", "\t1996-03-31 & 0.0155 & NA & 0.0258 & -0.0098 & NA & NA & NA & 0.0096 & -0.01057 & 0.00371 & 1996-03-31\\\\\n", "\t1996-04-30 & -0.0091 & NA & 0.0449 & 0.0236 & NA & NA & NA & 0.0147 & -0.01739 & 0.00428 & 1996-04-30\\\\\n", "\t1996-05-31 & 0.0076 & NA & 0.0353 & 0.0028 & NA & NA & NA & 0.0258 & -0.00543 & 0.00443 & 1996-05-31\\\\\n", "\t1996-06-30 & -0.0039 & NA & -0.0303 & -0.0019 & NA & NA & NA & 0.0038 & 0.01507 & 0.00412 & 1996-06-30\\\\\n", "\\end{tabular}\n" ], "text/plain": [ " HAM1 HAM2 HAM3 HAM4 HAM5 HAM6 EDHEC LS EQ SP500 TR\n", "1996-01-31 0.0074 NA 0.0349 0.0222 NA NA NA 0.0340\n", "1996-02-29 0.0193 NA 0.0351 0.0195 NA NA NA 0.0093\n", "1996-03-31 0.0155 NA 0.0258 -0.0098 NA NA NA 0.0096\n", "1996-04-30 -0.0091 NA 0.0449 0.0236 NA NA NA 0.0147\n", "1996-05-31 0.0076 NA 0.0353 0.0028 NA NA NA 0.0258\n", "1996-06-30 -0.0039 NA -0.0303 -0.0019 NA NA NA 0.0038\n", " US 10Y TR US 3m TR Dates\n", "1996-01-31 0.00380 0.00456 1996-01-31\n", "1996-02-29 -0.03532 0.00398 1996-02-29\n", "1996-03-31 -0.01057 0.00371 1996-03-31\n", "1996-04-30 -0.01739 0.00428 1996-04-30\n", "1996-05-31 -0.00543 0.00443 1996-05-31\n", "1996-06-30 0.01507 0.00412 1996-06-30" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Filled line Chart\n", "library(plotly)\n", "library(PerformanceAnalytics)\n", "\n", "#Load data\n", "data(managers)\n", "\n", "# Convert to data.frame\n", "managers.df <- as.data.frame(managers)\n", "managers.df$Dates <- index(managers)\n", "\n", "# See first few rows\n", "head(managers.df)\n", "\n", "# Plot\n", "p <- plot_ly(managers.df, x = ~Dates, y = ~HAM1, name = \"Manager 1\") %>% add_lines() \n", " layout(title = \"Time Series plot\")\n", "embed_notebook(p)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Heat map" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Heat map\n", "library(plotly)\n", "library(mlbench)\n", "\n", "# Get Sonar data\n", "data(Sonar)\n", "\n", "# Use only numeric data\n", "rock <- as.matrix(subset(Sonar, Class == \"R\")[,1:59])\n", "mine <- as.matrix(subset(Sonar, Class == \"M\")[,1:59])\n", "\n", "# For rocks\n", "p1 <- plot_ly(z = rock, type = \"heatmap\", showscale = F)\n", " \n", "# For mines\n", "p2 <- plot_ly(z = mine, type = \"heatmap\", name = \"test\") %>% \n", " layout(title = \"Mine vs Rock\")\n", "\n", "# Plot together\n", "p3 <- subplot(p1, p2)\n", "embed_notebook(p3)\n", "\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.2.3" } }, "nbformat": 4, "nbformat_minor": 0 }