This guide is a resource to explore data visualizations in R. The R programming language 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 .
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.
1Install.packages(“Package name”) 2library(library name) 3
For example, to install ggplot2:
1Install.packages(“ggplot2”) 2library(ggplot2) 3
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.
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.
1devtools::install_github("thomas85/patchwork") 2library(patchwork) 3
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.
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.
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.
This HTML Widget Library is especially for multiple regression using scatter plots. It is useful in creating easy facets and strips.
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.
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.
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.
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.
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.
This R wrapper is for robust and documented highcharts. It offers numerous charts with simple configuration syntax.
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.
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.
This package produces interactive maps using htmlwidgets framework.
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.
1dotchart(VADeaths,lables = row.names(VADeaths), col="red",pch=1, cex =.5)
|Dataset||Row Names as Yaxis Label||Change Point Color||Change Point Shape||Character Size|
|VADeaths||lables = row.names(VADeaths)||col="red"||pch=1||cex =.5|
1ggplot(mpg, aes(hwy)) + geom_density(aes(fill=factor(cyl)), alpha =0.8) + 2labs(title ="Highway Mileage Density plot",x="Highway Mileage",y="Density",fill = " No of Cylinders")
|Dataset||X-axis value||Select Density Plot||Y-axis Value||Change labels X-axis||Change labels Y-axis||Set sub-group of aesthetics|
|mpg||aes(hwy)||+geom_density()||aes(fill=factor(cyl)||x="Highway Mileage"||y="Density"||alpha =0.8|
1ggplot(mpg, aes(cty, hwy))+ geom_jitter(height = 2, width = 2) + geom_smooth(aes(col=manufacturer ) ,method = "lm" ,se =F)
|Dataset||X-axis & Y-axis value||Select Jitter Plot||Draw a line||Choose type of line(auto,lm,glm,loess,gam)||Confidence Interval (T/F)|
|mpg||aes(cty, hwy)||+ geom_jitter ()||geom_smooth ()||method = "lm"||se =F|
1ggplot(mpg, aes(x =`manufacturer`, y=`cty`, label= cty)) + 2 3 geom_point(stat='identity', fill="blue", size=6) + 4 geom_segment(aes(y = 0, x = `manufacturer`, yend = cty, xend = `manufacturer`), color = "blue") + geom_text(color="white", size=2) + 5 labs(title=" Lollipop Chart", subtitle="City Mileage")
|Dataset||X-axis & Y-axis value||Select Lollipop Plot||Set Text Color||Set Labels|
|mpg||aes( )||geom_point() geom_segment()||geom_text()||labs()|
1xyplot(mpg ~ hp | factor(cyl), data=mtcars,xlab = "Horse Power", ylab="Miles per gallon" ,main= "Effeciency of Cylinders" )
1cloud(cty ~ cyl*hwy | class ,data = mpg, 2 screen = list(x = -80, y = 90), distance = .6, zoom = .5)
1wireframe(cty ~ cyl * hwy, data = mpg, groups = trans,xlab = "cylinder", ylab= " Highway", zlab= "City", 2 scales = list(arrows = FALSE), 3 drape = TRUE, colorkey = TRUE, 4 screen = list(z = 40, x = -70))
The table below is a quick resource to a few more popular visualization choices.