assignmnet_plotly

mohamed morsy
23 / 6 / 2018

basic plotly

library(plotly)
plot_ly(mtcars, x = mtcars$wt, y = mtcars$mpg, mode = "markers" )

plot of chunk unnamed-chunk-1

colors

plot_ly(mtcars, x = mtcars$wt, y = mtcars$mpg,
        mode = "markers",
        color = mtcars$cyl)

plot of chunk unnamed-chunk-2

color factor

plot_ly(mtcars, x = mtcars$wt, y = mtcars$mpg,
        mode = "markers",
        color = as.factor(mtcars$cyl))

plot of chunk unnamed-chunk-3

continuous colors

plot_ly(mtcars, x = mtcars$wt, y = mtcars$mpg,
        mode = "markers",
        color = mtcars$disp,
        size = mtcars$hp)

plot of chunk unnamed-chunk-4

3d scatter plot

# webgl > opengl 
set.seed(2018-07-21)
temp <- rnorm(100, mean = 30, sd = 5)
pressure <- rnorm(100)
dtime <- 1:100
plot_ly(x = temp, y = pressure, z = dtime,
        type = "scatter3d", mode = "markers",
        color = temp)

plot of chunk unnamed-chunk-5

line graphs

data("airmiles")
plot_ly(x = time(airmiles), y= airmiles, mode = "line")

plot of chunk unnamed-chunk-6

multi line graph

library(tidyr);
library(dplyr);
data("EuStockMarkets");
stocks <- as.data.frame(EuStockMarkets) %>%
  gather(index, price) %>%  # from short format to long format data needed for plotly
  mutate(time = rep(time(EuStockMarkets), 4));

plot_ly(stocks, x= stocks$time, y = stocks$price,
        color = stocks$index)

plot of chunk unnamed-chunk-7

some examples

plot_ly(x = precip, type ="histogram")

plot of chunk unnamed-chunk-8

some examples

plot_ly(iris, y = iris$Petal.Length,
        color = iris$Species,
        type = "box")

plot of chunk unnamed-chunk-9

some examples

terrain <- matrix(rnorm(100*100), nrow = 100, ncol = 100)
plot_ly(z = terrain, type = "heatmap") # must be z

plot of chunk unnamed-chunk-10

some examples

terrain2 <- matrix(sort(rnorm(100*100)),
                   nrow = 100, ncol = 100)
map <- plot_ly(z = terrain2, type = "surface")

some examples

state_pop <- data.frame(State = state.abb,
                        Pop = as.vector(state.x77[,1]))
state_pop$hover <- with(state_pop,
                        paste(state_pop$State, '<br>',
                              "Population",
                              state_pop$Pop))

borders <- list (color = toRGB("red"))
map_options <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)
plot_ly(state_pop,
        z = state_pop$Pop,
        text = state_pop$hover,
        type = 'choropleth',
        locationmode = 'USA-states',
        color = state_pop$Pop,
        colors = 'Blues',
        marker = list(line = borders)) %>%
  layout(title = 'US Populationn in 1975',
         geo = map_options)

plot of chunk unnamed-chunk-12

plotly and ggplot

library(ggplot2)
set.seed(100)
data("diamonds")
d <- diamonds[sample(nrow(diamonds), 1000),]
p <- ggplot(data = d,
            aes (x = d$carat ,
                 y = d$price)) +
  geom_point(aes(text = paste("Clarity:", d$clarity)),
             size = 4) +
  geom_smooth(aes( colour = d$cut, fill = d$cut )) +
  facet_wrap(~ d$cut)

(gg <- ggplotly(p))

plot of chunk unnamed-chunk-13