Giter Club home page Giter Club logo

r-visual's Introduction

Data Visualization with R

Brought to you by Lesley Cordero.

Table of Contents

0.0 Setup

This guide was written in R 3.2.3.

0.1 R and R Studio

Download R and R Studio.

0.2 Packages

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")

0.4 Virtual Environment

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!

1.0 Plotting Basics

1.1 Plot Frames

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))

1.2 Labels

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()

1.3 Background

The default background is to have a plain white background. But that doesn't have to be the case.

1.3.1 Grids

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)

1.3.2 Images

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!

2.0 ggvis

ggvis allows you to visualize interactive plots from the makers of ggplot2.

library(ggvis)
iris %>% ggvis(~Petal.Length, ~Petal.Width, fill = ~Species) %>% layer_points()

3.0 ggplot2

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)

4.0 heatmaply

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))

5.0 plotly

plotly allows you to convert ggplot2 figures to interactive plots easily.

6.0 Final Words

6.1 Resources

r-visual's People

Contributors

schlosser avatar clesleycode avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.