Graphs: Plotly

2016/01/12

Plotly

# install.packages("plotly")
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble  3.0.3     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.0
## ✓ purrr   0.3.4
## ── Conflicts ────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag()    masks stats::lag()

Plotly example of horizontal barplot

## Plotly: barplot

# Creating a dataset
y <- c('The course was effectively<br>organized',
       'The course developed my<br>abilities and skills for<br>the subject',
       'The course developed my<br>ability to think critically about<br>the subject',
       'I would recommend this<br>course to a friend')
x1 <- c(21, 24, 27, 29)
x2 <-c(30, 31, 26, 24)
x3 <- c(21, 19, 23, 15)
x4 <- c(16, 15, 11, 18)
x5 <- c(12, 11, 13, 14)
d <- data_frame(y, x1, x2, x3, x4, x5)
## Warning: `data_frame()` is deprecated as of tibble 1.1.0.
## Please use `tibble()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
top_labels <- c('Strongly<br>agree', 'Agree', 'Neutral', 'Disagree', 'Strongly<br>disagree')

plot_ly(data = d,
        x = ~y,
        y = ~x1,
        # Bar settings
        type = "bar",
        orientation = "h")
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Adding traces:

```r
plot_ly(d, x = ~x1, y = ~y, type = 'bar', orientation = 'h',
    marker = list(color = 'rgba(38, 24, 74, 0.8)',
                  line = list(color = 'rgb(248, 248, 249)', width = 1))) %>%
add_trace(x = ~x2, marker = list(color = 'rgba(71, 58, 131, 0.8)')) %>%
add_trace(x = ~x3, marker = list(color = 'rgba(122, 120, 168, 0.8)')) %>%
add_trace(x = ~x4, marker = list(color = 'rgba(164, 163, 204, 0.85)')) %>%
add_trace(x = ~x5, marker = list(color = 'rgba(190, 192, 213, 1)'))
```

Ordered barplots

Initial problems looks like this

# Plotly: how to get bars in order?

# Dataframe
df <- structure(list(count = c(8, 3, 5, 9), 
                     names = c("I", "want", "this", "order"), 
                     type = c("H", "A", "H", "A")), 
                .Names = c("count","names", "type"), row.names = c(NA, -4L), class = "data.frame")

# Plotly plot
plot_ly(data=df,x=~names,y=~count,type="bar",color = ~type)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Then to fix this

df$names <- as.factor(df$names)

df$names <- factor(df$names, levels = unique(df$names)[order(df$names, decreasing = TRUE)])

# ordered
plot_ly(data=df, x=~names, y=~count, type="bar", color = ~type) %>% 
    layout(xaxis = list(categoryarray = ~count, categoryorder = "array"))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels