Creating and sa v ing more comple x plots DATA VISU AL IZATION IN - - PowerPoint PPT Presentation

creating and sa v ing more comple x plots
SMART_READER_LITE
LIVE PREVIEW

Creating and sa v ing more comple x plots DATA VISU AL IZATION IN - - PowerPoint PPT Presentation

Creating and sa v ing more comple x plots DATA VISU AL IZATION IN R Ron Pearson Instr u ctor Side effects and ret u rn v al u es All R graphics f u nctions are called for their side - e ects The y generate a plot Unlike most f u nctions ,


slide-1
SLIDE 1

Creating and saving more complex plots

DATA VISU AL IZATION IN R

Ron Pearson

Instructor

slide-2
SLIDE 2

DATA VISUALIZATION IN R

Side effects and return values

All R graphics functions are called for their side-eects They generate a plot Unlike most functions, they return nothing useful Exception: barplot() function

slide-3
SLIDE 3

DATA VISUALIZATION IN R

Side effects and return values

library(MASS) tbl <- table(UScereal$shelf) mids <- barplot(tbl, horiz = TRUE, col = "transparent", names.arg = "") mids [,1] [1,] 0.7 [2,] 1.9 [3,] 3.1 text(10, mids, names(tbl), col = "red", font = 2, cex = 2) title("Distribution of cereals by shelf")

slide-4
SLIDE 4

DATA VISUALIZATION IN R

symbols() shows relations between 3 or more variables

library(MASS) symbols(UScereal$sugars, UScereal$calories, squares = UScereal$shelf, inches = 0.1, bg = rainbow(3)[UScereal$shelf]) title("Cereal calories vs. sugars, coded by shelf")

slide-5
SLIDE 5

DATA VISUALIZATION IN R

Saving plots as png files

# Divert graphics output to png file png("SavedGraphicsFile.png") # Create the plot symbols(UScereal$sugars, UScereal$calories, squares = UScereal$shelf, inches = 0.1, bg = rainbow(3)[UScereal$shelf]) # Add the title title("Cereal calories vs. sugars, coded by shelf")

slide-6
SLIDE 6

Let's practice!

DATA VISU AL IZATION IN R

slide-7
SLIDE 7

Using color effectively

DATA VISU AL IZATION IN R

Ron Pearson

Instructor

slide-8
SLIDE 8

DATA VISUALIZATION IN R

Limitations of color

Color-blindness: not everyone can see colors Black-and-white reproduction loses all color-coded details Can be overused and lose usefulness

slide-9
SLIDE 9

DATA VISUALIZATION IN R

Iliinsky & Steele's recommended colors

"Ideally, about six ..." "... hopefully no more than 12 ..." "... and absolutely no more than 20"

slide-10
SLIDE 10

DATA VISUALIZATION IN R

Iliinsky & Steele's recommended colors

slide-11
SLIDE 11

DATA VISUALIZATION IN R

Iliinsky & Steele's recommended colors

slide-12
SLIDE 12

DATA VISUALIZATION IN R

Iliinsky & Steele's recommended colors

slide-13
SLIDE 13

Let's practice!

DATA VISU AL IZATION IN R

slide-14
SLIDE 14

Other graphics systems in R

DATA VISU AL IZATION IN R

Ron Pearson

Instructor

slide-15
SLIDE 15

DATA VISUALIZATION IN R

Why base R?

Flexible Good for exploratory analysis Easy to learn

slide-16
SLIDE 16

DATA VISUALIZATION IN R

The grid graphics system

Based on the grid package Greater control over low-level graphical details More exible than base graphics Comes at cost of steep learning curve

slide-17
SLIDE 17

DATA VISUALIZATION IN R

A simple scatterplot in grid

# Get the data and load the grid package library(MASS) x <- UScereal$sugars y <- UScereal$calories library(grid) # This is the grid code required to generate the plot pushViewport(plotViewport()) pushViewport(dataViewport(x, y)) grid.rect() grid.xaxis() grid.yaxis() grid.points(x, y) grid.text("UScereal$calories", x = unit(-3, "lines"), rot = 90) grid.text("UScereal$sugars", y = unit(-3, "lines"), rot = 0) popViewport(2)

slide-18
SLIDE 18

DATA VISUALIZATION IN R gridBase

Requires familiarity with both graphics systems

slide-19
SLIDE 19

DATA VISUALIZATION IN R

The lattice graphics system

Built on grid graphics Very good for conditional graphs

slide-20
SLIDE 20

DATA VISUALIZATION IN R

How does mpg vs horsepower vary by cylinders?

library(MASS) library(lattice) xyplot(MPG.city ~ Horsepower | Cylinders, data = Cars93)

slide-21
SLIDE 21

DATA VISUALIZATION IN R

The ggplot2 graphics package

Very popular graphics package based on grid graphics The bases for other DataCamp courses Allows us to build complex plots in stages

slide-22
SLIDE 22

DATA VISUALIZATION IN R

Example with ggplot2

# Sets up plot, but does not display it basePlot <- ggplot(UScereal, aes(x = sugars, y = calories) # Create a simple scatterplot basePlot + geom_point() # Make point shapes depend on shelf variable basePlot + geom_point(shape = as.character(UScereal$shelf)) # Make the points bigger, easier to see basePlot + geom_point(shape = as.character(UScereal$shelf), size = 3)

slide-23
SLIDE 23

Let's practice!

DATA VISU AL IZATION IN R