Author avatar

Jaya Trivedi

Cheat Sheet for R Data Visualizations

Jaya Trivedi

  • May 21, 2019
  • 8 Min read
  • 1,181 Views
  • May 21, 2019
  • 8 Min read
  • 1,181 Views
Data
R

Title

Introduction

This guide is a resource to explore data visualizations in R. R provides several packages to produce high-quality plots. In this guide, we will discuss a few popular choices. The resources for the other packages can be found in the resources section below. Before you begin with this guide, install RStudio and use “packages” search in the help tab to view all installed packages. The code for installing packages and libraries is given in each segment.

For a simple, single-line code visualization read the guide Approaches to select right Visualization for your data .

Install Packages

To install any of the packages below use the following syntax with the name of the library. A few are experimental, so the code to install is given below the description of the Package.

1
2
3
Install.packages(“Package name”)
library(library name)
 
r

For example, to install ggplot2:

1
2
3
Install.packages(“ggplot2”)
library(ggplot2)
 
r

GGPLOT2

This ‘Grammar of graphics’ based package is one of the most elegant and versatile. It is heavily popular among R users. The following three packages are based on ggplot and extend its capabilities.

Patchwork

The library helps combine ggplot2 objects’ ability to set columns, rows, and relative sizes of each component graphic. It provides an API to explore and iterate plots together.

1
2
3
devtools::install_github("thomas85/patchwork")
library(patchwork)
 
r

Ggiraph

Ggiraph helps animate ggplot geometries with an interactive Javascript onclick function. It is an html widget and accepts three arguments, tooltip, onclick, and data-id.

Esquisse

Esquisse helps provide a drag and drop interface for ggplot2. This add-in provides a quick and easy interface to visualize data and retrieve code, generating graphs. The only pitfall is that you cannot customize scales.

Dygraphs

This package provides a rich Javascript charting library to create time-series graphs. It can automatically graph data from an xts object. You can also add overlays like shaded regions, annotations, and event lines to the graph.

Googlevis

This package provides an interface between R and Google charts API. It requires an internet connection to produce charts. The inbuilt demo() outputs motion charts, geo maps, annotated time series, treemaps, and many others for preview.

Metricsgraphics

This library produces D3 graphics and can be embedded into Shiny web applications. The Chart types include interactive web scatter plots, histograms, along with line and bar charts.

Taucharts

This HTML Widget Library is especially for multiple regression using scatter plots. It is useful in creating easy facets and strips.

RColorBrewer

By using this library you can select pre-made color palettes for visualizations and produce colorful graphs. The palettes are divided into three categories: qualitative, diverging, and sequential.

Colourpicker

This built-in library for R for colors can be used with Shiny apps and R markdown documents. Use the plothelper() to pick colors for your plots.

Shiny

This is a package to create an interactive web application referred to as “Shiny apps”. It can take input from the user to customize the chart. It needs two components UI.R and UI.Server.

Flexdashboard

It is an interface to provide interactive dashboards to R users. It supports visualizations for mobile devices and has a storyboard layout to present sequential information.

Rcdimple

This package provides easy access to d3.js. The Htmlwidget helps in producing elegant charts without knowledge of Javascript.

1
Devtools::install_gitnub(“timelyportfolio/rcdimple”)
r

Plotly:ggplotly()

This library is built on plotly.js and works locally. It is included in the ggplot2 and extends its capability to produce interactive web-based graphs. The ggplotly() converts figures into plotly objects.

Highcharter

This R wrapper is for robust and documented highcharts. It offers numerous charts with simple configuration syntax.

Echarts4r

It helps to generate animated charts such as heat maps, sun charts, and geographical maps. It has the ability to create multiple chart grids and connect their inferences.

Lattice

This package is used for plotting inspired by trellis graphics. It is a useful tool for multivariate visualizations. It is built in with R so there is no need to install it.

Leaflet

This package produces interactive maps using htmlwidgets framework.

Build Your Visualizations

Given below are quick code blocks with parameters to produce the visualization shown. The datasets used are inbuilt in R, so there is no need to import them. Use the demo(colors) to see the color palette available or colors() to get a list of colors.

Dot charts

1
dotchart(VADeaths,lables = row.names(VADeaths), col="red",pch=1, cex =.5)
r
DatasetRow Names as Yaxis LabelChange Point ColorChange Point ShapeCharacter Size
VADeathslables = row.names(VADeaths)col="red"pch=1cex =.5

Dot Chart

ggplot2 Visualizations

Density Plot

1
2
ggplot(mpg, aes(hwy)) + geom_density(aes(fill=factor(cyl)), alpha =0.8) +
labs(title ="Highway Mileage Density plot",x="Highway Mileage",y="Density",fill = " No of Cylinders")
r
DatasetX-axis valueSelect Density PlotY-axis ValueChange labels X-axisChange labels Y-axisSet sub-group of aesthetics
mpgaes(hwy)+geom_density()aes(fill=factor(cyl)x="Highway Mileage"y="Density"alpha =0.8

Density Plot

Jitter Plot

1
ggplot(mpg, aes(cty, hwy))+ geom_jitter(height = 2, width = 2) + geom_smooth(aes(col=manufacturer ) ,method = "lm" ,se =F)
r
DatasetX-axis & Y-axis valueSelect Jitter PlotDraw a lineChoose type of line(auto,lm,glm,loess,gam)Confidence Interval (T/F)
mpgaes(cty, hwy)+ geom_jitter ()geom_smooth ()method = "lm"se =F

Jitter

Lollipop Plot

1
2
3
4
5
ggplot(mpg, aes(x =`manufacturer`, y=`cty`, label= cty)) +
 
  geom_point(stat='identity', fill="blue", size=6)  +
  geom_segment(aes(y = 0,  x = `manufacturer`, yend = cty,  xend = `manufacturer`),           	color = "blue") +  geom_text(color="white", size=2) +
  labs(title=" Lollipop Chart",  subtitle="City Mileage")
r
DatasetX-axis & Y-axis valueSelect Lollipop PlotSet Text ColorSet Labels
mpgaes( )geom_point() geom_segment()geom_text()labs()

Lollipop

Lattice Visualizations

Scatter Plot

1
xyplot(mpg ~ hp | factor(cyl), data=mtcars,xlab = "Horse Power", ylab="Miles per gallon" ,main= "Effeciency of Cylinders" )
r

ScatterPlot

3-D Scatter Plot

1
2
cloud(cty ~ cyl*hwy | class ,data = mpg,
  screen = list(x = -80, y = 90), distance = .6, zoom = .5)
r

Cloud

Wireframe

1
2
3
4
wireframe(cty ~ cyl * hwy, data = mpg, groups = trans,xlab = "cylinder", ylab= " Highway", zlab= "City",
      	scales = list(arrows = FALSE),
      	drape = TRUE, colorkey = TRUE,
      	screen = list(z = 40, x = -70))
r

Wireframe

Conclusion

The table below is a quick resource to a few more popular visualization choices.

conclusion

8