astra-sites
domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init
action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114jetpack
domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init
action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114wpforms-lite
domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init
action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114wordpress-seo
domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init
action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114astra
foi ativado muito cedo. Isso geralmente é um indicador de que algum código no plugin ou tema está sendo executado muito cedo. As traduções devem ser carregadas na ação init
ou mais tarde. Leia como Depurar o WordPress para mais informações. (Esta mensagem foi adicionada na versão 6.7.0.) in /home/statplace/public_html/site/wp-includes/functions.php on line 6114Artigo escrito em colabora\u00e7\u00e3o com Carolina Jacomini.<\/p>\n\n\n\n
Plotly \u00e9 uma empresa de computa\u00e7\u00e3o t\u00e9cnica (2012) com sede em Montreal, Quebec, que desenvolve an\u00e1lises de dados online e ferramentas de visualiza\u00e7\u00e3o. Plotly fornece gr\u00e1ficos, an\u00e1lises e ferramentas estat\u00edsticas para indiv\u00edduos e empresas, bem como bibliotecas de gr\u00e1ficos cient\u00edficos para Python, R, MATLAB, Perl, Julia, Arduino e REST.<\/p>\n\n\n\n
O pacote Plotly cria gr\u00e1ficos interativos da web<\/a> a partir de gr\u00e1ficos \u2018ggplot2\u2019 e\/ou uma interface personalizada para a biblioteca JavaScript (MITlicensed) \u2018plotly.js\u2019 inspirada na gram\u00e1tica dos gr\u00e1ficos. Para rodar os exemplos nesse artigo vamos usar o dplyr ent\u00e3o \u00e9 preciso ter o tidyverse j\u00e1 instalado<\/a>. <\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco utilizado \u00e9 o iris<\/em>. Ele cont\u00e9m tr\u00eas esp\u00e9cies de flores (setosa, virginica, versicolor) e outras 4 vari\u00e1veis relacionadas as suas medidas:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco de dados cont\u00e9m informa\u00e7\u00f5es das temperaturas m\u00e9dias (mais baixa e mais alta por dia) de Nova York por m\u00eas em 3 anos (2000, 2007, 2014). Sendo assim as vari\u00e1veis do banco s\u00e3o:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco utilizado \u00e9 o diamonds<\/em> do pacote ggplot2. Ele cont\u00e9m pre\u00e7os de mais de 50.000 cortes de diamantes. As vari\u00e1veis s\u00e3o:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n Tamb\u00e9m usa o banco de dados diamonds<\/em>.<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco de dados utilizado \u00e9 o ToothGrowth<\/em>. A resposta \u00e9 o comprimento dos odontoblastos (c\u00e9lulas respons\u00e1veis pelo crescimento dos dentes) em 60 cobaias. Cada porquinho-da-\u00edndia recebeu um dos tr\u00eas n\u00edveis de dose de vitamina C (0,5, 1 e 2 mg\/dia) por um dos dois m\u00e9todos de entrega, suco de laranja ou \u00e1cido asc\u00f3rbico. As vari\u00e1veis s\u00e3o:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n Duas vari\u00e1veis aleat\u00f3rias que seguem distribui\u00e7\u00e3o normal.<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n Tamb\u00e9m usa o pacote diamonds<\/em>.<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco de dados \u00e9 o USPersonalExpenditure<\/em>. Este conjunto de dados consiste em despesas pessoais dos Estados Unidos (em bilh\u00f5es de d\u00f3lares) para os anos de 1940, 1945, 1950, 1955 e 1960 nas categorias:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco de dados \u00e9 do github e ele cont\u00e9m 4 vari\u00e1veis:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n Os bancos de dados s\u00e3o do github e ambos s\u00e3o a respeito de sabores de caf\u00e9, sendo o segundo banco mais completo que o primeiro. As vari\u00e1veis s\u00e3o:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco de dados \u00e9 o mesmo utilizado anteriormente, relacionado \u00e0s universidades.<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco utilizado \u00e9 o mtcars<\/em>. Os dados foram extra\u00eddos da revista Motor Trend US de 1974 e abrangem o consumo de combust\u00edvel e 10 aspectos do design e desempenho do autom\u00f3vel para 32 autom\u00f3veis. As vari\u00e1veis s\u00e3o:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n Fazer uma pir\u00e2mide de base triangular.<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco de dados utilizado \u00e9 do Githube e ele cont\u00e9m as vari\u00e1veis de todos os pa\u00edses em 2014:<\/p>\n\n\n\n Exemplo<\/strong><\/p>\n\n\n\n O banco de dados utilizado \u00e9 do Githube e ele cont\u00e9m as vari\u00e1veis de todos as cidades dos EUA em 2014:<\/p>\n\n\n\n Banco de dados<\/strong><\/p>\n\n\n\n O pacote \u201cgapminder\u201d \u00e9 um trecho dos dados dispon\u00edveis em https:\/\/www.gapminder.org\/<\/a>. Para cada um dos 142 pa\u00edses, o pacote fornece valores para expectativa de vida, PIB per capita e popula\u00e7\u00e3o, a cada cinco anos, de 1952 a 2007.<\/p>\n\n\n\n O objetivo \u00e9 fazer um gr\u00e1fico que contenha todas as inform\u00e7\u00f5es desse banco de dados.<\/p>\n\n\n\n O banco de dados \u00e9 o txhousing<\/em>. Ele cont\u00e9m informa\u00e7\u00f5es sobre o mercado imobili\u00e1rio no Texas fornecidas pelo centro imobili\u00e1rio TAMU, http:\/\/recenter.tamu.edu\/<\/a>. As vari\u00e1veis s\u00e3o:<\/p>\n\n\n\n Podemos usar o plotly com o ggplot para tornar a visualiza\u00e7\u00e3o de dados interativa. Os gr\u00e1ficos saem do jeito que voc\u00ea j\u00e1 conhece no ggplot com algumas op\u00e7\u00f5es oferecidas pelo plotly. <\/p>\n\n\n\n Voc\u00ea pode ler mais sobre o pacote no site do plotly<\/a> ou ent\u00e3o j\u00e1 partir para o c\u00f3digo no R usando essa folha de atalhos com os gr\u00e1ficos <\/a>mais usados. <\/p>\n","protected":false},"excerpt":{"rendered":" O pacote Plotly cria gr\u00e1ficos interativos a partir de gr\u00e1ficos do ggplot2 e\/ou a biblioteca JavaScript \u2018plotly.js\u2019 inspirada na gram\u00e1tica dos gr\u00e1ficos.<\/p>\n","protected":false},"author":8,"featured_media":7012,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"","site-content-layout":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","spay_email":"","footnotes":""},"categories":[445],"tags":[73,284,247,285,286],"class_list":["post-6993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ferramentas-e-tecnologias","tag-grafico","tag-grafico-animado","tag-pacote","tag-plotly","tag-rstats-2"],"yoast_head":"\nlibrary(dplyr)\nlibrary(plotly)<\/code><\/pre>\n\n\n\n
Gr\u00e1ficos b\u00e1sicos<\/h2>\n\n\n\n
Scatter plot<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
head(iris)<\/code><\/pre>\n\n\n\n
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species\n## 1 5.1 3.5 1.4 0.2 setosa\n## 2 4.9 3.0 1.4 0.2 setosa\n## 3 4.7 3.2 1.3 0.2 setosa\n## 4 4.6 3.1 1.5 0.2 setosa\n## 5 5.0 3.6 1.4 0.2 setosa\n## 6 5.4 3.9 1.7 0.4 setosa<\/code><\/pre>\n\n\n\n
Scatter <- plot_ly(data = iris, \n x = ~Sepal.Length, \n y = ~Petal.Length, \n color = ~Species,\n type = 'scatter', \n mode = 'markers') %>%\n layout(xaxis = list(title = \"Comprimento da s\u00e9pala\"),\n yaxis = list(title = \"Comprimento da p\u00e9tala\")) \n \n\nScatter<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Line plot<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
head(Temp.New.york)<\/code><\/pre>\n\n\n\n
## month high_2000 low_2000 high_2007 low_2007 high_2014 low_2014\n## 1 January 32.5 13.8 36.5 23.6 28.8 12.7\n## 2 February 37.6 22.3 26.6 14.0 28.5 14.3\n## 3 March 49.9 32.5 43.6 27.0 37.0 18.6\n## 4 April 53.0 37.2 52.3 36.8 56.8 35.5\n## 5 May 69.1 49.9 71.5 47.6 69.7 49.9\n## 6 June 75.4 56.1 81.4 57.7 79.7 58.0<\/code><\/pre>\n\n\n\n
line <- plot_ly(Temp.New.york, \n x = ~month, \n y = ~high_2014, \n name = 'High 2014', \n type = 'scatter', \n mode = 'lines',\n line = list(color = 'rgb(205, 12, 24)', \n width = 4)) %>% \n add_trace(y = ~low_2014, \n name = 'Low 2014', \n line = list(color = 'rgb(22, 96, 167)', \n width = 4)) %>% \n add_trace(y = ~high_2007, \n name = 'High 2007', \n line = list(color = 'rgb(205, 12, 24)', \n width = 4, \n dash = 'dash')) %>% \n add_trace(y = ~low_2007, \n name = 'Low 2007', \n line = list(color = 'rgb(22, 96, 167)', \n width = 4, \n dash = 'dash')) %>% \n add_trace(y = ~high_2000, \n name = 'High 2000', \n line = list(color = 'rgb(205, 12, 24)', \n width = 4, \n dash = 'dot')) %>% \n add_trace(y = ~low_2000, \n name = 'Low 2000', \n line = list(color = 'rgb(22, 96, 167)', \n width = 4, \n dash = 'dot')) %>% \n layout(title = \"Temperaturas m\u00e9dias alta e baixa em New York\",\n xaxis = list(title = \"M\u00eas\"),\n yaxis = list (title = \"Temperatura (\u00baF)\"))\n\nline<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Density plot<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
library(ggplot2)\n\ndens <- with(diamonds, \n tapply(price, \n INDEX = cut, \n density))\n\ndata.dens <- data.frame(x = unlist(lapply(dens, \"[[\", \"x\")),\n y = unlist(lapply(dens, \"[[\", \"y\")),\n cut = rep(names(dens), \n each = length(dens[[1]]$x)))\n\nhead(data.dens)<\/code><\/pre>\n\n\n\n
## x y cut\n## Fair1 -1114.8694 6.744087e-08 Fair\n## Fair2 -1073.4981 9.022150e-08 Fair\n## Fair3 -1032.1268 1.196403e-07 Fair\n## Fair4 -990.7555 1.572893e-07 Fair\n## Fair5 -949.3842 2.064376e-07 Fair\n## Fair6 -908.0129 2.708506e-07 Fair<\/code><\/pre>\n\n\n\n
density <- plot_ly(data.dens, \n x = ~x, \n y = ~y, \n color = ~cut) %>% \n add_lines() %>%\n layout(xaxis = list(title = \"Pre\u00e7o\"),\n yaxis = list(title = \"\")) \n\ndensity<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
Bar chart<\/h3>\n\n\n\n
data.bar <- ggplot2::diamonds %>% \n count(cut,\n clarity)\n\nhead(data.bar)<\/code><\/pre>\n\n\n\n
## # A tibble: 6 x 3\n## cut clarity n\n## <ord> <ord> <int>\n## 1 Fair I1 210\n## 2 Fair SI2 466\n## 3 Fair SI1 408\n## 4 Fair VS2 261\n## 5 Fair VS1 170\n## 6 Fair VVS2 69<\/code><\/pre>\n\n\n\n
bar <- data.bar %>% \n plot_ly(x = ~cut, \n y = ~n, \n color = ~clarity) %>%\n layout(xaxis = list(title = \"Corte\"),\n yaxis = list(title = \"Frequ\u00eancia absoluta (N)\")) \n\nbar<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
Bar chart with error bars<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
library(plyr)\n\ndata_mean <- ddply(ToothGrowth, \n c(\"supp\", \n \"dose\"), \n summarise, \n length = mean(len))\n\ndata_sd <- ddply(ToothGrowth, \n c(\"supp\", \n \"dose\"), \n summarise, \n length = sd(len))\n\nvitamin <- data.frame(data_mean, \n data_sd$length)\n\nvitamin <- rename(vitamin, \n c(\"data_sd.length\" = \"sd\"))\n\nvitamin$dose <- as.factor(vitamin$dose)\n\nhead(vitamin)<\/code><\/pre>\n\n\n\n
## supp dose length sd\n## 1 OJ 0.5 13.23 4.459709\n## 2 OJ 1 22.70 3.910953\n## 3 OJ 2 26.06 2.655058\n## 4 VC 0.5 7.98 2.746634\n## 5 VC 1 16.77 2.515309\n## 6 VC 2 26.14 4.797731<\/code><\/pre>\n\n\n\n
error <- plot_ly(data = vitamin[which(vitamin$supp == 'OJ'),], \n x = ~dose, \n y = ~length, \n type = 'bar', \n name = 'OJ',\n error_y = ~list(array = sd,\n color = '#000000')) %>% \n add_trace(data = vitamin[which(vitamin$supp == 'VC'),], \n name = 'VC') %>%\n layout(xaxis = list(title = \"Dose\"),\n yaxis = list(title = \"Comprimento\")) \n\nerror<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Histogram<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
plot_ly(alpha = 0.6) %>% \n add_histogram(x = ~rnorm(500)) %>% \n add_histogram(x = ~rnorm(500) + 1) %>% \n layout(barmode = \"overlay\")<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Boxplots<\/h3>\n\n\n\n
head(diamonds)<\/code><\/pre>\n\n\n\n
## # A tibble: 6 x 10\n## carat cut color clarity depth table price x y z\n## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>\n## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43\n## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31\n## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31\n## 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63\n## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75\n## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48<\/code><\/pre>\n\n\n\n
plot_ly(diamonds, \n y = ~price, \n color = ~cut, \n type = \"box\")<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Pie chart<\/h3>\n\n\n\n
USPersonalExpenditure <- data.frame(\"Categorie\" = rownames(USPersonalExpenditure), \n USPersonalExpenditure)\n\ndata.pie <- USPersonalExpenditure[, c('Categorie', \n 'X1960')]\n\nhead(data.pie)<\/code><\/pre>\n\n\n\n
## Categorie X1960\n## Food and Tobacco Food and Tobacco 86.80\n## Household Operation Household Operation 46.20\n## Medical and Health Medical and Health 21.10\n## Personal Care Personal Care 5.40\n## Private Education Private Education 3.64<\/code><\/pre>\n\n\n\n
colors <- c('rgb(211,94,96)', \n 'rgb(128,133,133)', \n 'rgb(144,103,167)', \n 'rgb(171,104,87)', \n 'rgb(114,147,203)')\n\npie <- plot_ly(data.pie, \n labels = ~Categorie, \n values = ~X1960, \n type = 'pie',\n textposition = 'inside',\n textinfo = 'label+percent',\n insidetextfont = list(color = '#FFFFFF'),\n hoverinfo = 'text',\n text = ~paste('$', \n X1960, \n ' billions'),\n marker = list(colors = colors,\n line = list(color = '#FFFFFF', \n width = 1)),\n showlegend = FALSE) %>% \n layout(title = 'United States Personal Expenditures by Categories in 1960',\n xaxis = list(showgrid = FALSE, \n zeroline = FALSE, \n showticklabels = FALSE),\n yaxis = list(showgrid = FALSE, \n zeroline = FALSE, \n showticklabels = FALSE))\n\npie<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Bubble chart<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
data.bubble <- read.csv(\"https:\/\/raw.githubusercontent.com\/plotly\/datasets\/master\/school_earnings.csv\")\n\nhead(data.bubble)<\/code><\/pre>\n\n\n\n
## School Women Men Gap\n## 1 MIT 94 152 58\n## 2 Stanford 96 151 55\n## 3 Harvard 112 165 53\n## 4 U.Penn 92 141 49\n## 5 Princeton 90 137 47\n## 6 Chicago 78 118 40<\/code><\/pre>\n\n\n\n
bubble <- plot_ly(data.bubble, \n x = ~Women, \n y = ~Men, \n text = ~School, \n type = 'scatter', \n mode = 'markers',\n marker = list(size = ~Gap, \n opacity = 0.5)) %>% \n layout(title = 'Gender Gap in Earnings per University',\n xaxis = list(showgrid = FALSE),\n yaxis = list(showgrid = FALSE))\n\nbubble<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Gr\u00e1ficos diferentes<\/h2>\n\n\n\n
Sunburst chart<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
d1 <- read.csv('https:\/\/raw.githubusercontent.com\/plotly\/datasets\/master\/coffee-flavors.csv')\n\nhead(d1)<\/code><\/pre>\n\n\n\n
## ids labels parents\n## 1 Enzymatic-Flowery Flowery \n## 2 Enzymatic-Fruity Fruity \n## 3 Enzymatic-Herby Herby \n## 4 Sugar Browning-Nutty Nutty \n## 5 Sugar Browning-Carmelly Carmelly \n## 6 Sugar Browning-Chocolatey Chocolatey<\/code><\/pre>\n\n\n\n
d2 <- read.csv('https:\/\/raw.githubusercontent.com\/plotly\/datasets\/718417069ead87650b90472464c7565dc8c2cb1c\/sunburst-coffee-flavors-complete.csv')\n\nhead(d2)<\/code><\/pre>\n\n\n\n
## ids labels parents\n## 1 Aromas Aromas \n## 2 Tastes Tastes \n## 3 Aromas-Enzymatic Enzymatic Aromas\n## 4 Aromas-Sugar Browning Sugar Browning Aromas\n## 5 Aromas-Dry Distillation Dry Distillation Aromas\n## 6 Tastes-Bitter Bitter Tastes<\/code><\/pre>\n\n\n\n
sunburst <- plot_ly() %>%\n add_trace(ids = d1$ids,\n labels = d1$labels,\n parents = d1$parents,\n type = 'sunburst',\n maxdepth = 2,\n domain = list(column = 0)) %>%\n add_trace(ids = d2$ids,\n labels = d2$labels,\n parents = d2$parents,\n type = 'sunburst',\n maxdepth = 3,\n domain = list(column = 1)) %>%\n layout(grid = list(columns = 2, \n rows = 1),\n margin = list(l = 0, \n r = 0,\n b = 0, \n t = 0),\n sunburstcolorway = c(\"#636efa\",\"#EF553B\",\"#00cc96\",\"#ab63fa\",\"#19d3f3\",\n \"#e763fa\", \"#FECB52\",\"#FFA15A\",\"#FF6692\",\"#B6E880\"),\n extendsunburstcolors = TRUE)\nsunburst<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Dumbbell plot<\/h3>\n\n\n\n
Data.dumbbell <- read.csv(\"https:\/\/raw.githubusercontent.com\/plotly\/datasets\/master\/school_earnings.csv\")\n\nData.dumbbell$School <- factor(Data.dumbbell$School, \n levels = Data.dumbbell$School[order(Data.dumbbell$Men)])\n\n\nhead(Data.dumbbell)<\/code><\/pre>\n\n\n\n
## School Women Men Gap\n## 1 MIT 94 152 58\n## 2 Stanford 96 151 55\n## 3 Harvard 112 165 53\n## 4 U.Penn 92 141 49\n## 5 Princeton 90 137 47\n## 6 Chicago 78 118 40<\/code><\/pre>\n\n\n\n
Dumbbell <- plot_ly(Data.dumbbell,\n color = I(\"gray80\")) %>% \n add_segments(x = ~Women, \n xend = ~Men, \n y = ~School, \n yend = ~School, \n showlegend = FALSE) %>% \n add_markers(x = ~Women, \n y = ~School, \n name = \"Women\", \n color = I(\"pink\")) %>% \n add_markers(x = ~Men, \n y = ~School, \n name = \"Men\", \n color = I(\"blue\")) %>% \n layout(title = \"Gender earnings disparity\",\n xaxis = list(title = \"Annual Salary (in thousands)\"),\n margin = list(l = 65))\n\nDumbbell<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
3D Scatter plot<\/h3>\n\n\n\n
mtcars$am[which(mtcars$am == 0)] <- 'Automatic'\n\nmtcars$am[which(mtcars$am == 1)] <- 'Manual'\n\nmtcars$am <- as.factor(mtcars$am)\n\nhead(mtcars)<\/code><\/pre>\n\n\n\n
## mpg cyl disp hp drat wt qsec vs am gear carb\n## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 Manual 4 4\n## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 Manual 4 4\n## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 Manual 4 1\n## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 Automatic 3 1\n## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 Automatic 3 2\n## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 Automatic 3 1<\/code><\/pre>\n\n\n\n
Scatter3D <- plot_ly(mtcars, \n x = ~wt, \n y = ~hp, \n z = ~qsec, \n color = ~am, \n colors = c('#BF382A', \n '#0C4B8E')) %>% \n add_markers() %>% \n layout(scene = list(xaxis = list(title = 'Weight'),\n yaxis = list(title = 'Gross horsepower'),\n zaxis = list(title = '1\/4 mile time')))\n\nScatter3D<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
3D Surface Plot<\/h3>\n\n\n\n
<\/figure>\n\n\n\n
Surface <- plot_ly(x = c(0, 1, 2, 0),\n y = c(0, 0, 1, 2),\n z = c(0, 2, 0, 1),\n i = c(0, 0, 0, 1),\n j = c(1, 2, 3, 2),\n k = c(2, 3, 1, 3),\n facecolor = toRGB(viridisLite::viridis(4)))\n\nSurface<\/code><\/pre>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Mapas usando plotly<\/h2>\n\n\n\n
Choropleth map<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Data.choropleth <- read.csv('https:\/\/raw.githubusercontent.com\/plotly\/datasets\/master\/2014_world_gdp_with_codes.csv')\n\nhead(Data.choropleth)<\/code><\/pre>\n\n\n\n
## COUNTRY GDP..BILLIONS. CODE\n## 1 Afghanistan 21.71 AFG\n## 2 Albania 13.40 ALB\n## 3 Algeria 227.80 DZA\n## 4 American Samoa 0.75 ASM\n## 5 Andorra 4.80 AND\n## 6 Angola 131.40 AGO<\/code><\/pre>\n\n\n\n
l <- list(color = toRGB(\"grey\"), \n width = 0.5)\n\ng <- list(showframe = FALSE,\n showcoastlines = FALSE,\n projection = list(type = 'Mercator'))\n\nChoropleth <- plot_geo(Data.choropleth) %>% \n add_trace(z = ~GDP..BILLIONS., \n color = ~GDP..BILLIONS., \n colors = 'Blues',\n text = ~COUNTRY, \n locations = ~CODE, \n marker = list(line = l)) %>% \n colorbar(title = 'GDP Billions US$', \n tickprefix = '$') %>% \n layout(title = '2014 Global GDP<br>Source:<ahref=\"https:\/\/www.cia.gov\/library\/publications\/the-world-factbook\/fields\/2195.html\">CIA World Factbook<\/a>',\n geo = g)\n\nChoropleth<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
Bubble Map<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
Data.bmap <- read.csv('https:\/\/raw.githubusercontent.com\/plotly\/datasets\/master\/2014_us_cities.csv')\n\nData.bmap$q <- with(Data.bmap, \n cut(pop, \n quantile(pop)))\n\nlevels(Data.bmap$q) <- paste(c(\"1st\", \n \"2nd\", \n \"3rd\", \n \"4th\", \n \"5th\"), \n \"Quantile\")\nData.bmap$q <- as.ordered(Data.bmap$q)\n\nhead(Data.bmap)<\/code><\/pre>\n\n\n\n
## name pop lat lon q\n## 1 New York 8287238 40.73060 -73.98658 4th Quantile\n## 2 Los Angeles 3826423 34.05372 -118.24273 4th Quantile\n## 3 Chicago 2705627 41.87555 -87.62442 4th Quantile\n## 4 Houston 2129784 29.75894 -95.36770 4th Quantile\n## 5 Philadelphia 1539313 39.95233 -75.16379 4th Quantile\n## 6 Phoenix 1465114 33.44677 -112.07567 4th Quantile<\/code><\/pre>\n\n\n\n
g <- list(scope = 'usa',\n projection = list(type = 'albers usa'),\n showland = TRUE,\n landcolor = toRGB(\"gray85\"),\n subunitwidth = 1,\n countrywidth = 1,\n subunitcolor = toRGB(\"white\"),\n countrycolor = toRGB(\"white\"))\n\nBubble.map <- plot_geo(Data.bmap, \n locationmode = 'USA-states', \n sizes = c(1, 250)) %>% \n add_markers(x = ~lon, \n y = ~lat, \n size = ~pop, \n color = ~q, \n hoverinfo = \"text\",\n text = ~paste(Data.bmap$name, \"<br \/>\", \n Data.bmap$pop\/1e6, \n \" million\")) %>% \n layout(title = '2014 US city populations<br>(Click legend to toggle)', \n geo = g)\n\nBubble.map<\/code><\/pre>\n\n\n\n
## Warning: `line.width` does not currently support multiple values.\n\n## Warning: `line.width` does not currently support multiple values.\n\n## Warning: `line.width` does not currently support multiple values.\n\n## Warning: `line.width` does not currently support multiple values.<\/code><\/pre>\n\n\n\n
Gr\u00e1ficos animados no plotly<\/h2>\n\n\n\n
Exemplo: Gr\u00e1fico de bolhas<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
library(gapminder)\n\nBanco.de.dados <- gapminder \nhead(Banco.de.dados)<\/code><\/pre>\n\n\n\n
## # A tibble: 6 x 6\n## country continent year lifeExp pop gdpPercap\n## <fct> <fct> <int> <dbl> <int> <dbl>\n## 1 Afghanistan Asia 1952 28.8 8425333 779.\n## 2 Afghanistan Asia 1957 30.3 9240934 821.\n## 3 Afghanistan Asia 1962 32.0 10267083 853.\n## 4 Afghanistan Asia 1967 34.0 11537966 836.\n## 5 Afghanistan Asia 1972 36.1 13079460 740.\n## 6 Afghanistan Asia 1977 38.4 14880372 786.<\/code><\/pre>\n\n\n\n
Objetivo<\/h3>\n\n\n\n
figura <- Banco.de.dados %>%\n plot_ly(x = ~gdpPercap, \n y = ~lifeExp, \n size = ~pop, \n color = ~continent, \n frame = ~year, \n text = ~country, \n hoverinfo = \"text\",\n type = 'scatter',\n mode = 'markers') %>% \n layout(xaxis = list(type = \"log\",\n title = \"PIB per capita\"),\n yaxis = list(title = \"Expectativa de vida\"))\n\nfigura<\/code><\/pre>\n\n\n\n
animation_opts<\/h3>\n\n\n\n
figura2 <- figura %>%\n animation_opts(frame = 1000, \n easing = 'sin')\n\nfigura2<\/code><\/pre>\n\n\n\n
animation_button<\/h3>\n\n\n\n
figura3 <- figura2 %>%\n animation_button(x = 1,\n xanchor = \"right\",\n y = 0,\n yanchor = \"bottom\")\n\nfigura3<\/code><\/pre>\n\n\n\n
animation_slider<\/h3>\n\n\n\n
figura4 <- figura3 %>%\n animation_slider(currentvalue = list(prefix = \"YEAR \", \n font = list(color=\"red\")))\n\nfigura4<\/code><\/pre>\n\n\n\n
Exemplo: gr\u00e1fico de linhas<\/h2>\n\n\n\n
Banco de dados<\/h3>\n\n\n\n
<\/figure><\/div>\n\n\n\n
library(plotly)\n\naccumulate_by <- function(dat, var) {\n var <- lazyeval::f_eval(var, dat)\n lvls <- plotly:::getLevels(var)\n dats <- lapply(seq_along(lvls), function(x) {\n cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])\n })\n dplyr::bind_rows(dats)\n}\n\nData.txhousing <- txhousing %>%\n filter(year > 2005, \n city %in% c(\"Abilene\", \"Bay Area\")) %>% \n accumulate_by(~date)\n\nhead(Data.txhousing)<\/code><\/pre>\n\n\n\n
## city year month sales volume median listings inventory date\n## 1 Abilene 2006 1 107 11890000 90800 559 3.4 2006.000\n## 2 Bay Area 2006 1 367 58297417 133100 3038 5.7 2006.000\n## 3 Abilene 2006 1 107 11890000 90800 559 3.4 2006.000\n## 4 Abilene 2006 2 152 16875000 98200 561 3.4 2006.083\n## 5 Bay Area 2006 1 367 58297417 133100 3038 5.7 2006.000\n## 6 Bay Area 2006 2 471 77665581 138900 3157 5.8 2006.083\n## frame\n## 1 2006.000\n## 2 2006.000\n## 3 2006.083\n## 4 2006.083\n## 5 2006.083\n## 6 2006.083<\/code><\/pre>\n\n\n\n
Gr\u00e1fico final<\/h3>\n\n\n\n
fig <- Data.txhousing %>%\n plot_ly(x = ~date, \n y = ~median,\n split = ~city,\n frame = ~frame, \n type = 'scatter',\n mode = 'lines', \n line = list(simplyfy = F)) %>% \n layout(xaxis = list(title = \"Date\",\n zeroline = F),\n yaxis = list(title = \"Median\",\n zeroline = F)) %>% \n animation_opts(frame = 100,\n transition = 0,\n redraw = FALSE) %>% \n animation_slider(hide = T) %>% \n animation_button(x = 1, \n xanchor = \"right\", \n y = 0, \n yanchor = \"bottom\")\n\nfig<\/code><\/pre>\n\n\n\n