some examples in r- [data visualization--r graphics]

19
Some Examples in R- [Data Visualization--R graphics] Rcharts and others.. library(rpart.plot),library(rCharts),library(plyr),library(knitr), library(reshape2),library(scales) Reference: https://github.com/ramnathv/rCharts/tree/master/inst/libraries http://www.rpubs.com/dnchari/rcharts rCharts package by Ramnath Vaidyanathan

Upload: volkan-oban

Post on 16-Apr-2017

78 views

Category:

Data & Analytics


1 download

TRANSCRIPT

Page 1: Some Examples in R- [Data Visualization--R graphics]

Some Examples in R- [Data Visualization--R graphics]

Rchartsand others..library(rpart.plot),library(rCharts),library(plyr),library(knitr),library(reshape2),library(scales)

Reference:

 https://github.com/ramnathv/rCharts/tree/master/inst/libraries

http://www.rpubs.com/dnchari/rcharts

rCharts package by Ramnath Vaidyanathan

Prepared by Volkan OBAN

CODES:

Page 2: Some Examples in R- [Data Visualization--R graphics]

require(rCharts) ## Example 1

names(iris) = gsub("\\.", "", names(iris))

rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')

Example2-

## Example 2 Facetted Barplot

hair_eye = as.data.frame(HairEyeColor)

rPlot(Freq ~ Hair | Eye, color = 'Eye', data = hair_eye, type = 'bar')

Page 3: Some Examples in R- [Data Visualization--R graphics]

Example3-

require(rCharts)

> names(iris) = gsub("\\.", "", names(iris))> rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')> ## Example 2 Facetted Barplot> hair_eye = as.data.frame(HairEyeColor)> rPlot(Freq ~ Hair | Eye, color = 'Eye', data = hair_eye, type = 'bar')> hair_eye = as.data.frame(HairEyeColor)> p2 <- nPlot(Freq ~ Hair, group = 'Eye',+ data = subset(hair_eye, Sex == "Female"),+ type = 'multiBarChart'+ )> p2$chart(color = c('brown', 'blue', '#594c26', 'green'))> p2

Page 4: Some Examples in R- [Data Visualization--R graphics]

Example 4:

p <- nPlot(~ cyl, data = mtcars, type = 'pieChart')> p$chart(donut = TRUE> p5

Page 5: Some Examples in R- [Data Visualization--R graphics]

Example 5:

data(economics, package = 'ggplot2')> p <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')> p

Page 6: Some Examples in R- [Data Visualization--R graphics]

Example 6:

dat <- data.frame(

t = rep(0:23, each = 4),

var = rep(LETTERS[1:4], 4),

val = round(runif(4*24,0,50)) )

p<- nPlot(val ~ t, group = 'var', data = dat, type = 'stackedAreaChart', id = 'chart' )

p

Page 7: Some Examples in R- [Data Visualization--R graphics]
Page 8: Some Examples in R- [Data Visualization--R graphics]
Page 9: Some Examples in R- [Data Visualization--R graphics]

Example 7:

require(rCharts)> p1 <- rPlot(SOG ~ yearID, data = team_data,+ type = "point",+ size = list(const = 2),+ color = list(const = "#888"),+ tooltip = "#! function(item){+ return item.SOG + ' ' + item.name + ' ' + item.yearID+ } !#"+ )> p1

Page 10: Some Examples in R- [Data Visualization--R graphics]

> myteam = "Boston Red Sox"> p1$layer(data = team_data[team_data$name == myteam,],+ color = list(const = 'red'),+ copy_layer = T)> p1$set(dom = 'chart3')> p1>

Page 11: Some Examples in R- [Data Visualization--R graphics]

Example 8:

> > library(rCharts)> library(plyr)> library(knitr)> library(reshape2)> library(scales)> options(RCHART_WIDTH = 600, RCHART_HEIGHT = 400)> knitr::opts_chunk$set(comment = NA, results = 'asis', tidy = F, message = F)> data(tips, package = 'reshape2')

p= rPlot(x = 'day', y = 'box(tip)', data = tips, type = 'box')> p

Page 12: Some Examples in R- [Data Visualization--R graphics]

nba = read.csv('http://datasets.flowingdata.com/ppg2008.csv')> nba.m = ddply(melt(nba), .(variable), transform, rescale = rescale(value))> p= rPlot(Name ~ variable, color = 'rescale', data = nba.m, type = 'tile', height = 600)> p

Page 13: Some Examples in R- [Data Visualization--R graphics]

Example:

haireye = as.data.frame(HairEyeColor)> dat = subset(haireye, Sex == "Female" & Eye == "Blue")> haireye = as.data.frame(HairEyeColor)> dat = subset(haireye, Sex == "Female" & Eye == "Blue")> p1 = mPlot(x = 'Hair', y = list('Freq'), data = dat, type = 'Bar', labels = list("Count"))> p1

Page 14: Some Examples in R- [Data Visualization--R graphics]

dat = subset(haireye, Sex == "Female")

p2 = mPlot(Freq ~ Eye, group = "Hair", data = dat, type = "Bar", labels = 1:4)

p2

Page 15: Some Examples in R- [Data Visualization--R graphics]

Example:

data(economics, package = 'ggplot2')

dat = transform(economics, date = as.character(date))

p3 <- mPlot(x = "date", y = list("psavert", "uempmed"), data = dat, type = 'Line',

pointSize = 0, lineWidth = 1)

p3$set(xLabelFormat = "#! function (x) {

return x.toString(); }

!#")

p3$set(type = 'Area')

p3

Page 16: Some Examples in R- [Data Visualization--R graphics]

Example: ecm = reshape2::melt(economics[,c('date', 'uempmed', 'psavert')], id = 'date')

p7 = nPlot(value ~ date, group = 'variable', data = ecm, type = 'lineWithFocusChart')

p7$xAxis( tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#" )

p7

Page 17: Some Examples in R- [Data Visualization--R graphics]

Example:

library(rpart.plot)h= hPlot(x = "Wr.Hnd", y = "NW.Hnd", data = MASS::survey, + type = c("line", "bubble", "scatter"), group = "Clap", size = "Age")h> map3 = Leaflet$new()> map3$setView(c(41.7627, -72.6743), zoom = 13)> map3$marker(c(41.7627, -72.6743), bindPopup = "<p> Hi. I am a popup </p>")> map3$marker(c(41.70, -72.60), bindPopup = "<p> Hi. I am another popup </p>")> map3> map3 = Leaflet$new()> map3$setView(c(41.7627, -72.6743), zoom = 13)> map3$marker(c(41.7627, -72.6743), bindPopup = "<p> Hi. I am a popup </p>")> map3$marker(c(41.70, -72.60), bindPopup = "<p> Hi. I am another popup </p>")> map3Example:

Page 18: Some Examples in R- [Data Visualization--R graphics]

> map3 = Leaflet$new()> map3$setView(c(41.7627, -72.6743), zoom = 13)> map3$marker(c(41.7627, -72.6743), bindPopup = "<p> Hi. I am a popup </p>")> map3$marker(c(41.70, -72.60), bindPopup = "<p> Hi. I am another popup </p>")> map3

Reference:

 https://github.com/ramnathv/rCharts/tree/master/inst/libraries

http://www.rpubs.com/dnchari/rcharts

rCharts package by Ramnath Vaidyanathan

Page 19: Some Examples in R- [Data Visualization--R graphics]