These data visualisation modules provide an overview of packages for interactive graphs with R.
See here for the course materials.
For other R libraries, see leaflet, htmlwidgets, plotly and useful tutorials on data visualisation in R at flowingdata.com.
Many of the packages below require that you have Rtools installed in addition to base R. I also recommend using RStudio as a front end.
The rCharts package allows you to create, customize and publish interactive javascript visualizations from R using a lattice style plotting interface.
To get started, open R and install and load the package as follows.
library(devtools)
install_github('ramnathv/rCharts')
Then you can start exploring the examples using pre-loaded datasets or your own data. For more examples, check out the rCharts gallery.
library(rCharts)
source('https://raw.githubusercontent.com/walkerke/teaching-with-datavis/master/pyramids/rcharts_pyramids.R')
## Select population counts for Qatar in 2014
dat <- getAgeTable(country='QA', year=2014)
kable(head(dat))
Year | Age | total | Male | Female | percent | pctMale | pctFemale | sexratio | ord | |
---|---|---|---|---|---|---|---|---|---|---|
2 | 2014 | 0-4 | 101272 | 51253 | 50019 | 4.8 | 3.1 | 10.3 | 102.5 | 1 |
3 | 2014 | 5-9 | 88869 | 45308 | 43561 | 4.2 | 2.8 | 9.0 | 104.0 | 2 |
4 | 2014 | 10-14 | 74976 | 37916 | 37060 | 3.5 | 2.3 | 7.6 | 102.3 | 3 |
5 | 2014 | 15-19 | 87906 | 55349 | 32557 | 4.1 | 3.4 | 6.7 | 170.0 | 4 |
6 | 2014 | 20-24 | 196261 | 152929 | 43332 | 9.2 | 9.3 | 8.9 | 352.9 | 5 |
7 | 2014 | 25-29 | 322775 | 259540 | 63235 | 15.2 | 15.8 | 13.0 | 410.4 | 6 |
n1 <- nPyramid('QA', 2014, colors = c('darkred', 'silver'))
n1$show('inline', include_assets = TRUE, cdn = TRUE)
## Load data
SchoolsGroup <- read.csv("https://raw.github.com/patilv/rChartsTutorials/master/SchoolsGroup.csv")
kable(head(SchoolsGroup))
X | Group.1 | Group.2 | Group.3 | Group.4 |
---|---|---|---|---|
1 | Texas | Cincinnati | Kentucky | Bowling Green |
2 | Ohio State | Memphis | Louisville | William & Mary |
3 | Michigan | Air Force | South Carolina | Montana |
4 | Alabama | South Florida | Minnesota | North Dakota |
5 | Florida | Boise State | Washington | South Alabama |
6 | Texas A&M | New Mexico | North Carolina | Maine |
## Load styles
tab2 <- dTable(SchoolsGroup[,-1], sPaginationType = "full_numbers")
tab2$templates$script <- "http://timelyportfolio.github.io/rCharts_dataTable/chart_customsort.html"
## Set column names
tab2$params$table$aoColumns =
list(
list(sType = "string_ignore_null", sTitle = "Group 1"),
list(sType = "string_ignore_null", sTitle = "Group 2"),
list(sType = "string_ignore_null", sTitle = "Group 3"),
list(sType = "string_ignore_null", sTitle = "Group 4")
)
## Produce data table
n1 <- tab2
n1$show('inline', include_assets = TRUE, cdn = TRUE)
The googleVis package provides an interface to Google Charts API, allowing users to create interactive charts based on R data frames.
## Load the googleVis library
library(googleVis)
## Inspect data
kable(head(Regions))
Region | Parent | Val | Fac |
---|---|---|---|
Global | NA | 10 | 2 |
America | Global | 2 | 4 |
Europe | Global | 99 | 11 |
Asia | Global | 10 | 8 |
France | Europe | 71 | 2 |
Sweden | Europe | 89 | 3 |
## Plot
Tree <- gvisTreeMap(data = Regions,
idvar = "Region", parentvar = "Parent",
sizevar = "Val", colorvar = "Fac",
options=list(fontSize=16))
plot(Tree)
datSK <- data.frame(From=c(rep("A",3), rep("B", 3)),
To=c(rep(c("X", "Y", "Z"),2)),
Weight=c(5,7,6,2,9,4))
kable(head(datSK))
From | To | Weight |
---|---|---|
A | X | 5 |
A | Y | 7 |
A | Z | 6 |
B | X | 2 |
B | Y | 9 |
B | Z | 4 |
Sankey <- gvisSankey(data = datSK, from="From", to="To", weight="Weight",
options=list(
sankey="{link: {color: { fill: '#d799ae' } },
node: { color: { fill: '#a61d4c' },
label: { color: '#871b47' } }}"))
plot(Sankey)
kable(head(Cairo))
Date | Temp | |
---|---|---|
2558 | 2002-01-01 | 13.61111 |
2559 | 2002-01-02 | 15.16667 |
2560 | 2002-01-03 | 12.00000 |
2561 | 2002-01-04 | 12.00000 |
2562 | 2002-01-05 | 13.05556 |
2563 | 2002-01-06 | 10.05556 |
Cal <- gvisCalendar(data = Cairo,
datevar="Date",
numvar="Temp",
options=list(
title="Daily temperature in Cairo",
height=320,
calendar="{yearLabel: { fontName: 'Times-Roman',
fontSize: 32, color: '#1A8763', bold: true},
cellSize: 10,
cellColor: { stroke: 'red', strokeOpacity: 0.2 },
focusedCellColor: {stroke:'red'}}")
)
plot(Cal)
kable(head(CityPopularity))
City | Popularity |
---|---|
New York | 200 |
Boston | 300 |
Miami | 400 |
Chicago | 500 |
Los Angeles | 600 |
Houston | 700 |
Pie <- gvisPieChart(data = CityPopularity)
plot(Pie)
kable(head(Exports))
Country | Profit | Online |
---|---|---|
Germany | 3 | TRUE |
Brazil | 4 | FALSE |
United States | 5 | TRUE |
France | 4 | TRUE |
Hungary | 3 | FALSE |
India | 2 | TRUE |
Geo <- gvisGeoChart(data = Exports, locationvar="Country",
colorvar="Profit",
options=list(projection="kavrayskiy-vii"))
plot(Geo)
kable(head(Fruits))
Fruit | Year | Location | Sales | Expenses | Profit | Date |
---|---|---|---|---|---|---|
Apples | 2008 | West | 98 | 78 | 20 | 2008-12-31 |
Apples | 2009 | West | 111 | 79 | 32 | 2009-12-31 |
Apples | 2010 | West | 89 | 76 | 13 | 2010-12-31 |
Oranges | 2008 | East | 96 | 81 | 15 | 2008-12-31 |
Bananas | 2008 | East | 85 | 76 | 9 | 2008-12-31 |
Oranges | 2009 | East | 93 | 80 | 13 | 2009-12-31 |
M <- gvisMotionChart(data = Fruits, idvar = 'Fruit', timevar = 'Year',
options=list(width=400, height=350))
plot(M)