Highcharts is a very good JavaScript library for creating cutomized time series, bar charts, column charts etc. The R library rCharts has a wrapper function for Highcharts and you can most of the features from Highcharts.
Install rCharts:
require(devtools)
install_github('ramnathv/rCharts')
library(rCharts)
data("iris")
p1 <- hPlot(Sepal.Length ~ Sepal.Width, data = iris, group = "Species", type = "scatter")
p1$colors('rgba(223, 83, 83, 1)', 'rgba(119, 152, 191, 1)', 'rgba(60, 179, 113, 1)')
p1$title(text = "Edgar Anderson's Iris Data")
p1
data("longley")
## Initialize a new Highcharts object
p2 <- Highcharts$new()
## Specify the chart type
p2$chart(type = "line")
## Add data to the chart
p2$series(data = as.list(longley$Employed), name = "Employed")
p2$series(data = as.list(longley$Unemployed), name = "Unemployed")
p2$xAxis(categories = longley$Year)
## Add titles
p2$xAxis(title = list(text = "Year"))
p2$yAxis(title = list(text = "Number of people"))
p2$title(text = "Longley's Economic Regression Data")
p2
Make the plot zoomable:
## You can also set zoomType as "x", "y" or "xy"
p2$chart(zoomType = "x")
p2$subtitle(text = "Click and drag in the plot area to zoom in")
p2
## Death rates per 1000 in Virginia in 1940
data("VADeaths")
## Tranform data from matrix to data.frame, and add a variable for age group
VADeaths_df <- as.data.frame(VADeaths)
VADeaths_df <- cbind(VADeaths_df, age = row.names(VADeaths))
## Initialize a new Highcharts object
p3 <- Highcharts$new()
## Specify the chart type
p3$chart(type = "column")
## Add data to the chart
p3$series(data = as.list(VADeaths_df$`Rural Male`), name = "Rural Male")
p3$series(data = as.list(VADeaths_df$`Rural Female`), name = "Rural Female")
p3$series(data = as.list(VADeaths_df$`Urban Male`), name = "Urban Male")
p3$series(data = as.list(VADeaths_df$`Urban Female`), name = "Urban Female")
p3$xAxis(categories = VADeaths_df$age)
## Add titles
p3$xAxis(title = list(text = "Age group"))
p3$yAxis(title = list(text = "Death rates per 1000"))
p3$title(text = "Death rates in Virginia in 1940")
p3
Add customized tooltip:
p3$tooltip(shared = TRUE)
p3$plotOptions(column = list(pointPadding = 0.2, borderwidth = 0))
p3
## Data sets related to the 50 states of the United States of America.
data("state")
## Create a list of data
data_series <- list()
for (i in 1:length(state.abb)) {
data_series[[i]] <- list(x = state.x77[i, "Illiteracy"],
y = state.x77[i, "HS Grad"],
z = state.x77[i, "Income"],
name = state.abb[i],
state = state.name[i])
}
## formatter function for tooltip
formattter_function <- "#! function(){
return '<b>State:</b> ' + this.point.state +
'<br><b>Illiteracy:</b> ' + this.point.x +
'<br><b>High-school graduates:</b> ' + this.point.y +
'<br><b>Income:</b> ' + this.point.z;} !#"
## Initialize a new Highcharts object
p4 <- Highcharts$new()
## Specify the chart type
p4$chart(type = "bubble", plotBorderWidth = 1, zoomType = "xy")
## Add data to the chart
p4$series(data = data_series)
## Customize tooltip
p4$tooltip(formatter = formattter_function)
## Add state abbreviation into the bubbles
p4$plotOptions(series = list(dataLabels = list(enabled = TRUE, format = "{point.name}")))
## Disable legend
p4$legend(enabled = FALSE)
## Add titles
p4$title(text = "Data sets related to the 50 states of the United States of America.")
p4$subtitle(text = "Click and drag in the plot area to zoom in")
p4$xAxis(title = list(text = "Illiteracy (1970, percent of population)"))
p4$yAxis(title = list(text = "Percent high-school graduates (1970)"))
p4
p5 <- Highcharts$new()
p5$chart(type = "line", polar = TRUE)
p5$series(data = as.list(c(43000, 19000, 60000, 35000, 17000, 10000)), name = "Allocated Budget")
p5$series(data = as.list(c(50000, 39000, 42000, 31000, 26000, 14000)), name = "Actual Spending")
p5$xAxis(categories = c('Sales', 'Marketing', 'Development', 'Customer Support',
'Information Technology', 'Administration'),
tickmarkPlacement = "on",
lineWidth = 0)
p5$yAxis(gridLineInterpolation = 'polygon',
tickmarkPlacement = "on",
lineWidth = 0)
p5$tooltip(shared = TRUE,
pointFormat = "<span style='color:{series.color}'>{series.name}: <b>${point.y:,.0f}</b><br/>")
p5$title(text = 'Budget vs spending')
p5
With plotly, you can create interactive plots with existing ggplot without effort.
library(plotly)
ggplot_object <- ggplot(data = mtcars, aes(x = mpg, y = disp)) + geom_line() +
ggtitle("Motor Trend Car Road Tests") + xlab("Miles/(US) gallon") + ylab("Displacement (cu.in.)")
ggplot_object
ggplotly(ggplot_object)
threejs is a library to visualize 3D data.
library(threejs)
z <- seq(-10, 10, 0.1)
x <- cos(z)
y <- sin(z)
scatterplot3js(x,y,z, color=rainbow(length(z)),
labels=sprintf("x=%.2f, y=%.2f, z=%.2f", x, y, z))
data("flights")
## Approximate locations as factors
dest <- factor(sprintf("%.2f:%.2f",flights[,3], flights[,4]))
## A table of destination frequencies
freq <- sort(table(dest), decreasing=TRUE)
## The most frequent destinations in these data, possibly hub airports?
frequent_destinations <- names(freq)[1:10]
## Subset the flight data by destination frequency
idx <- dest %in% frequent_destinations
frequent_flights <- flights[idx, ]
## Lat/long and counts of frequent flights
ll <- unique(frequent_flights[,3:4])
## Plot frequent destinations as bars, and the flights to and from
## them as arcs. Adjust arc width and color by frequency.
globejs(lat=ll[,1], long=ll[,2], arcs=frequent_flights,
arcsHeight=0.3, arcsLwd=2, arcsColor="#ffff00", arcsOpacity=0.15,
atmosphere=TRUE, color="#00aaff", pointsize=0.5)
visNetwork is a package for network visualization, using vis.js javascript library.
visNetwork needs at least two informations:
library(visNetwork)
nodes <- data.frame(id = 1:10,
label = LETTERS[1:10],
group = sample(c(0, 1), 10, replace = TRUE),
shape = sample(c("box", "circle"), 10, replace = TRUE))
edges <- data.frame(from = sample(1:10, 15, replace = TRUE),
to = sample(1:10, 15, replace = TRUE))
visNetwork(nodes, edges)
The googleVis package provides an interface between R and the Google Charts API.
Cal <- gvisCalendar(Cairo,
datevar = "Date",
numvar = "Temp",
options = list(
title = "Daily temperature in Cairo",
calendar = "{yearLabel: {fontName: 'Times-Roman',
fontSize: 32,
color: '#030303',
bold: true},
cellSize: 17,
cellColor: {stroke: 'red',
strokeOpacity: 0.2},
focusedCellColor: {stroke:'red'}}"))
print(Cal, "chart")
Pie <- gvisPieChart(CityPopularity, labelvar = "City", numvar = "Popularity",
options=list(slices = "{4: {offset: 0.2}, 0: {offset: 0.3}}",
title = 'City popularity',
legend = 'none',
pieSliceText = 'label',
pieHole = 0.5))
print(Pie, "chart")
PopTable <- gvisTable(Population,
formats = list(Population = "#,###",
'% of World Population' = '#.#%'),
options = list(page='enable'))
print(PopTable, "chart")