Brought to you by Lesley Cordero.
This guide was written in R 3.2.3.
To install the R packages, cd
into your workspace, and enter the following, very simple, command into your bash:
R
This will prompt a session in R! From here, you can install any needed packages. For the sake of this tutorial, enter the following into your terminal R session:
install.packages("ggvis")
install.packages("heatmaply")
install.packages("png")
If you'd like to work in a virtual environment, you can set it up as follows:
pip3 install virtualenv
virtualenv your_env
And then launch it with:
source your_env/bin/activate
To execute the visualizations in matplotlib, do the following:
cd ~/.matplotlib
vim matplotlibrc
And then, write backend: TkAgg
in the file. Now you should be set up with your virtual environment!
Cool, now we're ready to start!
Visualizations are created on plot frames. To initialize a plot frame, you can write the following:
plot.new()
or
frame()
Now, if you want to customize the sizes of a plotframe, you can do so as follows:
pWidth = 3
pHeight = 2
plot.window(c(0,pWidth),
c(0,pHeight))
As with any visualization, you're going to want to attach labels:
mtext("x-axis",
side=1) #Add text to the x-axis
mtext("y-axis",
side=2)
title("An R Plot") #Add a title
You might also want to add a box frame for the visualizations with:
box()
The default background is to have a plain white background. But that doesn't have to be the case.
For some purposes, you might find it necessary to include a grid in your plot. You can easily add a grid to your plot by using the grid()
function. For example, as follows:
x <- c(1,2,3,4,5)
y <- 2*x
plot(x,y)
grid(10,10)
More exciting though, is the fact that you can make the background an image of your choice. In this example, I'll use the png
module to put the Byte Academy logo as the background.
library(png)
First, you want to load in the image. Use the readPNG()
function to specify the path to the picture:
image <- readPNG("./byte.png")
Next, you want to set up the plot area and call the par()
function:
plot(1:2, type='n', main="Plotting Over an Image", xlab="x", ylab="y")
lim <- par()
You can use the par()
function to set the graphical parameters in rasterImage()
. You use the argument usr
to define the extremes of the user coordinates of the plotting region. In this case, you put 1, 3, 2 and 4:
rasterImage(image, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
Next, you draw a grid and add some lines:
grid()
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="red")
And there you have it!
ggvis allows you to visualize interactive plots from the makers of ggplot2.
library(ggvis)
iris %>% ggvis(~Petal.Length, ~Petal.Width, fill = ~Species) %>% layer_points()
ggplot2 is one of the best static visualization packages in R.
ggplot(mpg, aes(displ, 1 / hwy)) +
geom_point() +
geom_smooth(method = lm, aes(colour = "lm"), se = FALSE) +
geom_smooth(aes(colour = "loess"), se = FALSE)
heatmaply produces interactive heatmaps.
library(heatmaply)
heatmaply(cor(mtcars),
k_col = 2, k_row = 2,
limits = c(-1,1)) %>%
layout(margin = list(l = 40, b = 40))
plotly allows you to convert ggplot2 figures to interactive plots easily.