Guest User

Untitled

a guest
Aug 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. ## ------------------------------------------------------------------------
  2. ## Make sure you can install these!!!
  3. ## ------------------------------------------------------------------------
  4. ## install.packages("quantmod")
  5. ## install.packages("RColorBrewer")
  6. ## install.packages("devtools")
  7. ## # if you encounter errors, let me know!
  8. ## devtools::install_github("ropensci/plotly#628")
  9.  
  10. ## ------------------------------------------------------------------------
  11. c(0, 1)
  12.  
  13. ## ------------------------------------------------------------------------
  14. x <- c(0, 1)
  15.  
  16. ## ------------------------------------------------------------------------
  17. sum(x + 10)
  18.  
  19. ## ------------------------------------------------------------------------
  20. x <- list(a = 1:5, b = "red")
  21. x$a
  22. x[["a"]]
  23. x["a"]
  24.  
  25. ## ------------------------------------------------------------------------
  26. range(mtcars$mpg)
  27.  
  28. ## ------------------------------------------------------------------------
  29. library(plotly)
  30. p <- qplot(data = mpg, displ, hwy, geom = c("point", "smooth")) + facet_wrap(~drv)
  31. ggplotly(p)
  32.  
  33. ## ------------------------------------------------------------------------
  34. str(volcano)
  35.  
  36. ## ------------------------------------------------------------------------
  37. str(~volcano)
  38. f <- function() { str(~volcano) }
  39. f()
  40.  
  41. ## ---- message = TRUE, fig.width=5, fig.height=4.5------------------------
  42. plot_ly(z = ~volcano)
  43.  
  44. ## ---- eval = FALSE-------------------------------------------------------
  45. ## # equivalent to before, but more explicit
  46. ## add_heatmap(plot_ly(z = ~volcano))
  47.  
  48. ## ---- eval = FALSE-------------------------------------------------------
  49. ## plot_ly(z = ~volcano) %>% add_surface() %>% plotly_POST()
  50.  
  51. ## ---- eval = FALSE-------------------------------------------------------
  52. ## plotly_POST(add_surface(plot_ly(z = ~volcano)))
  53.  
  54. ## ------------------------------------------------------------------------
  55. library(plotly)
  56. txhousing
  57.  
  58. ## ---- message = TRUE-----------------------------------------------------
  59. plot_ly(txhousing, x = ~sales)
  60.  
  61. ## ------------------------------------------------------------------------
  62. library(dplyr)
  63. d <- txhousing %>%
  64. group_by(city) %>%
  65. summarise(m = mean(sales, na.rm = TRUE))
  66. d
  67.  
  68. ## ------------------------------------------------------------------------
  69. txhousing %>%
  70. group_by(city) %>%
  71. summarise(m = mean(sales, na.rm = TRUE)) %>%
  72. ### <b>
  73. arrange(m) %>%
  74. plot_ly(x = ~m, y = ~city) %>%
  75. layout(title ="Average monthly sales, by city", margin = list(l = 120))
  76. ### </b>
  77.  
  78. ## ------------------------------------------------------------------------
  79. p <- txhousing %>%
  80. group_by(city) %>%
  81. plot_ly(x = ~date, y = ~sales, text = ~city) %>%
  82. layout(hovermode = "closest")
  83. p
  84.  
  85. ## ------------------------------------------------------------------------
  86. add_lines(p, y = ~log(sales))
  87.  
  88. ## ---- echo = FALSE-------------------------------------------------------
  89. p %>%
  90. add_lines(y = ~log(sales), color = ~city,
  91. line = list(color = "black"), hoverinfo = "text") %>%
  92. layout(hovermode = "closest", showlegend = FALSE) %>%
  93. # currently requires some JavaScript, hopefully it won't in the future!
  94. htmlwidgets::onRender('
  95. function(el, x) {
  96. var graphDiv = document.getElementById(el.id);
  97. // reduce the opacity of every trace except for the hover one
  98. el.on("plotly_hover", function(e) {
  99. var traces = [];
  100. for (var i = 0; i < x.data.length; i++) {
  101. if (i !== e.points[0].curveNumber) traces.push(i);
  102. }
  103. Plotly.restyle(graphDiv, "opacity", 0.2, traces);
  104. })
  105. el.on("plotly_unhover", function(e) {
  106. var traces = [];
  107. for (var i = 0; i < x.data.length; i++) traces.push(i);
  108. Plotly.restyle(graphDiv, "opacity", 1, traces);
  109. })
  110. }
  111. ')
  112.  
  113. ## ---- echo = FALSE-------------------------------------------------------
  114. p %>%
  115. add_lines(y = ~median, color = ~city, text = ~city,
  116. line = list(color = "black"), hoverinfo = "text") %>%
  117. layout(hovermode = "closest", showlegend = FALSE) %>%
  118. # currently requires some JavaScript, hopefully it won't in the future!
  119. htmlwidgets::onRender('
  120. function(el, x) {
  121. var graphDiv = document.getElementById(el.id);
  122. // reduce the opacity of every trace except for the hover one
  123. el.on("plotly_hover", function(e) {
  124. var traces = [];
  125. for (var i = 0; i < x.data.length; i++) {
  126. if (i !== e.points[0].curveNumber) traces.push(i);
  127. }
  128. Plotly.restyle(graphDiv, "opacity", 0.2, traces);
  129. })
  130. el.on("plotly_unhover", function(e) {
  131. var traces = [];
  132. for (var i = 0; i < x.data.length; i++) traces.push(i);
  133. Plotly.restyle(graphDiv, "opacity", 1, traces);
  134. })
  135. }
  136. ')
  137.  
  138. ## ------------------------------------------------------------------------
  139. top5 <- txhousing %>%
  140. group_by(city) %>%
  141. summarise(m = mean(sales, na.rm = TRUE)) %>%
  142. arrange(desc(m)) %>%
  143. top_n(5)
  144. top5
  145.  
  146. ## ------------------------------------------------------------------------
  147. top5plot <- txhousing %>%
  148. semi_join(top5) %>%
  149. plot_ly(x = ~date, y = ~sales, color = ~city)
  150. add_lines(top5plot)
  151.  
  152. ## ------------------------------------------------------------------------
  153. RColorBrewer::display.brewer.all(type = "qual")
  154.  
  155. ## ------------------------------------------------------------------------
  156. add_lines(top5plot, colors = "Dark2")
  157.  
  158. ## ------------------------------------------------------------------------
  159. txhousing %>%
  160. semi_join(top5) %>%
  161. group_by(city) %>%
  162. do(p = plot_ly(., x = ~log(sales), name = .$city))
  163.  
  164. ## ------------------------------------------------------------------------
  165. txhousing %>%
  166. semi_join(top5) %>%
  167. group_by(city) %>%
  168. do(p = plot_ly(., x = ~log(sales), name = ~city)) %>%
  169. ### <b>
  170. .[["p"]] %>%
  171. subplot(nrows = 5, shareX = TRUE)
  172. ### </b>
  173.  
  174. ## ------------------------------------------------------------------------
  175. economics
  176.  
  177. ## ------------------------------------------------------------------------
  178. library(tidyr)
  179. gather(economics, variable, value, -date)
  180.  
  181. ## ------------------------------------------------------------------------
  182. economics %>%
  183. gather(variable, value, -date) %>%
  184. group_by(variable) %>%
  185. summarise(m = min(value))
  186.  
  187. ## ------------------------------------------------------------------------
  188. economics %>%
  189. gather(variable, value, -date) %>%
  190. group_by(variable) %>%
  191. do(p = plot_ly(., x = ~date, y = ~value, name = ~variable)) %>%
  192. .[["p"]] %>%
  193. subplot(nrows = NROW(.), shareX = TRUE, titleX = FALSE)
  194.  
  195. ## ------------------------------------------------------------------------
  196. economics %>%
  197. plot_ly(x = ~uempmed, y = ~unemploy/pop) %>%
  198. add_markers(color = ~as.numeric(date), text = ~date, hoverinfo = "text")
  199.  
  200. ## ------------------------------------------------------------------------
  201. library(quantmod)
  202. msft <- getSymbols("MSFT", auto.assign = F)
  203. dat <- as.data.frame(msft)
  204. dat$date <- index(msft)
  205. head(dat)
  206.  
  207. names(dat) <- sub("^MSFT\\.", "", names(dat))
  208.  
  209. plot_ly(dat, x = ~date, xend = ~date, color = ~Close > Open,
  210. colors = c("red", "forestgreen"), hoverinfo = "none") %>%
  211. add_segments(y = ~Low, yend = ~High, line = list(width = 1)) %>%
  212. add_segments(y = ~Open, yend = ~Close, line = list(width = 3)) %>%
  213. layout(showlegend = FALSE, yaxis = list(title = "Price")) %>%
  214. toWebGL()
Add Comment
Please, Sign In to add comment