Loading Libraries

library(knitr)
library(printr)
library(rCharts)

HighCharts

Data

head(MASS::survey)
Sex Wr.Hnd NW.Hnd W.Hnd Fold Pulse Clap Exer Smoke Height M.I Age
Female 18.5 18.0 Right R on L 92 Left Some Never 173.00 Metric 18.250
Male 19.5 20.5 Left R on L 104 Left None Regul 177.80 Imperial 17.583
Male 18.0 13.3 Right L on R 87 Neither None Occas NA NA 16.917
Male 18.8 18.9 Right R on L NA Neither None Never 160.00 Metric 20.333
Male 20.0 20.0 Right Neither 35 Right Some Never 165.00 Metric 23.667
Female 18.0 17.7 Right L on R 64 Right Some Never 172.72 Imperial 21.000

\(\\\)

Plot 1: Multi- shape scatter plot

library(MASS)
h1 <- hPlot(x = "Wr.Hnd", y = "NW.Hnd", data = survey, 
            type = c("line", "bubble", "scatter"), 
            group = "Clap", 
            size = "Age")

h1$show('inline', include_assets = TRUE)

\(\\\)

Plot 2: Scatter plot with groups

h2 <- hPlot(Pulse ~ Height, data = survey, 
            type = "scatter", group = "Exer")

h2$show('inline', include_assets = TRUE)

\(\\\)

Plot 3: Bubble plot

h3 <- hPlot(Pulse ~ Height, data = survey, 
            type = "bubble", 
            group = "Exer",
            title = "Pulse vs. Heigh chart ", 
            subtitle = "bubble chart",
            size = "Age", 
            group = "Exer")

h3$show('inline', include_assets = TRUE)

\(\\\)

Plot 4: Pie chart

x <- data.frame(key = c("a", "b", "c"), value = c(1, 2, 3))

h4 <- hPlot(x = "key", y = "value", data = x, type = "pie")

h4$show('inline', include_assets = TRUE)

\(\\\)

Plot 5: Scatter plot

h5<- hPlot(Pulse ~ Height, data = survey, 
           type = 'scatter', 
           group = 'Sex', 
           radius = 6, 
           group.na = "Not Available")

h5$colors('rgba(223, 83, 83, .5)', 
          'rgba(119, 152, 191, .5)', 
          'rgba(60, 179, 113, .5)')

h5$legend(align = 'right', verticalAlign = 'top', layout = 'vertical')
h5$plotOptions(scatter = list(marker = list(symbol = 'circle')))
h5$tooltip(formatter = "#! function() { return this.x + ', ' + this.y; } !#")

h5$show('inline', include_assets = TRUE)

\(\\\)

Plot 6: Bar and line plot

Data for plot:

library(plyr)
data <- count(survey, c('Sex', 'Exer'))
data
Sex Exer freq
Female Freq 49
Female None 11
Female Some 58
Male Freq 65
Male None 13
Male Some 40
NA Freq 1

Plot:

h6 <- hPlot(freq ~ Exer, data = data, 
            type = c('column', 'line'), 
            group = 'Sex', 
            radius = 6)

h6$show('inline', include_assets = TRUE)

\(\\\)

Plot 7: Horizontal grouped bar plot

Data for plot:

library(plyr)
data <- count(survey, c('Sex', 'Exer'))
data
Sex Exer freq
Female Freq 49
Female None 11
Female Some 58
Male Freq 65
Male None 13
Male Some 40
NA Freq 1

Plot:

h7 <- hPlot(freq ~ Exer, data = data, 
            type = 'bar', 
            group = 'Sex', 
            group.na = 'NA\'s')

h7$show('inline', include_assets = TRUE)

\(\\\)

Plot 8: Vertical grouped bar plot with annotation

h8 <- hPlot(freq ~ Exer, data = data, 
            type = 'column', 
            group = 'Sex', 
            group.na = 'NA\'s')

h8$plotOptions(column = list(dataLabels = 
                                     list(enabled = T, rotation = -90, align = 'right', 
                                          color = '#FFFFFF', x = 4, y = 10, 
                                          style = list(fontSize = '13px', 
                                                       fontFamily = 'Verdana, sans-serif'))))

h8$xAxis(labels = list(rotation = -45, 
                       align = 'right', 
                       style = list(fontSize = '13px', 
                                    fontFamily = 'Verdana, sans-serif')), 
         replace = F)

h8$show('inline', include_assets = TRUE)

\(\\\)

Plot 9: Horizontal grouped bar plot with pointer and click

h9 <- hPlot(freq ~ Exer, data = data, 
           type = 'bar', 
           group = 'Sex', 
           group.na = 'NA\'s')

h9$plotOptions(bar = list(cursor = 'pointer', 
                         point = list(events = list(click = "#! function() { alert ('Category: '+ this.category +', value: '+ this.y); } !#"))))

h9$show('inline', include_assets = TRUE)

\(\\\)

Plot 10

h10 <- Highcharts$new()
h10$series(data = list(
    list(y = 8, url = "https://github.com/metagraf/rHighcharts", color = "lightblue"),
    list(y = 14, url = "https://github.com/metagraf/rVega", color = "lightpink"),
    list(y = 71, url = "https://github.com/ramnathv/rCharts", color = "lightgreen")), 
    type = "column", name = "Number of Stars")

h10$plotOptions(column = list(cursor = 'pointer', 
                              point = list(events = list(click = "#! function() { location.href = this.options.url; } !#"))))

h10$xAxis(categories = c("rHighcharts", "rVega", "rCharts"), 
        title = list(text = ""))

h10$yAxis(title = list(text = ""))

h10$legend(enabled = F)

h10$show('inline', include_assets = TRUE)

Reference