O Plotly traduz de maneira fácil gráficos do ggplot2 para uma versão interativa na web ou cria uma visualização na web customizável diretamente do R via uma uma biblioteca de código aberto para JavaScript (plotly.js).
a função ggplotly() tentará replicar fielmente a versão estética do ggplot2.
a função plot_ly() permite uma interface mais direta com o plotly.js de modo que é possível criar gráficos especializados como mapas ou visualizações que o API do ggplot2 nunca suportará, como superfícies, malhas ou diagramas de Sankey.
Pacotes necessários:
library(magrittr)
library(knitr)
library(dplyr)
library(shiny)
library(plotly)
library(devtools)
library(readxl)Observações:
plot_ly() (transforma os dados em um objeto plotly) e ggploty()(transforma um objeto ggplot em um objeto plotly );A biblioteca gráfica R de Plotly faz gráficos interativos de qualidade de publicação on-line.
Sys.setenv("plotly_username"="biapinna")
Sys.setenv("plotly_api_key"="***")Tipos de gráficos 2D: scatter, bar, box, heatmap, histogram, histogram2d, area, pie, contour.
p1<-plot_ly(diamonds, y = ~price, color = ~cut, type = "box");p1p2<-plot_ly (diamonds, x=~cut , y =~carat, type = "bar" , color = ~color )%>%
layout(title = "Barplot",xaxis = list(title= "Quality of the cut"),yaxis = list(title= "Weight of the diamond"));p2#publicação ERRO de tamanho
#api_create(p2,filename = "exemplo2", sharing = "public")p3 <- plot_ly(diamonds, x = ~carat, y = ~price, type="scatter", symbol = ~clarity, symbols = c("0", "diamond-cross-open", "square-cross-open" , "x-open-dot" ,"triangle-down-open-dot" , "y-down-open" , "asterisk-open" , "circle-open-dot"),colors=c("red","blue","pink","orange","purple","green","black","gray"),marker = list(size = 15)) %>% layout(title = "Scatterplot", autosize = T);p3## Warning: The shape palette can deal with a maximum of 6 discrete values
## because more than 6 becomes difficult to discriminate; you have 8.
## Consider specifying shapes manually if you must have them.
#publicação
#api_create(p3,filename = "exemplo3", sharing = "public")link exemplo 3: https://plot.ly/~biapinna/29
OBS: O gratuito tem um tamanho máximo por gráfico (talvez 524KB), 25 gráficos no total e exporta somente em PNG e JPEG.
r1<-plot_ly(data = diamonds, x = ~cut, y = ~price,
type = "box")
r2<-plot_ly(data = diamonds, x = ~color, y = ~price,
type = "box")
r3<-plot_ly(data = diamonds, x = ~clarity, y = ~price,
type = "box")
p.sm1 <- subplot(r1, r2, r3, nrows=1)%>% layout(showlegend = FALSE);p.sm1#ou
p.sm2<-subplot(r1, r2,r3, shareY = TRUE, widths = c(0.4, 0.3,0.3), margin = 0) %>% hide_legend()
p.sm2Boxplot
ggplot()p<-ggplot(diamonds, aes(cut,price)) +
geom_boxplot(aes(fill=cut));pggploty()p1<-ggplotly(p);p1plot_ly()plot_ly(diamonds, y = ~price, color = ~cut) %>%
add_boxplot()#opção sem o operador pipe
plot_ly(diamonds, y = ~price, color = ~cut, type = "box")Histograma
ggplot()p<-ggplot(diamonds, aes(price, fill = cut)) +
geom_histogram(binwidth = 50,position = "dodge");pggploty()p1<-ggplotly(p);p1plot_ly()plot_ly(diamonds, x = ~price, color = ~cut) %>%
add_histogram()#opção sem o operador pipe
plot_ly(diamonds, x = ~price, color = ~cut, type = "histogram")https://bookdown.org/paulcbauer/idv2/9-1-simple-example.html
https://plotly-book.cpsievert.me/plot-ly-for-collaboration.html
https://plotly-book.cpsievert.me/.
https://plot.ly/r/shiny-tutorial/#plotly-graphs-in-shiny
https://bookdown.org/paulcbauer/idv2/7-plotly-in-r.html
https://www.datacamp.com/community/open-courses/plotly-tutorial-plotly-and-r