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 , the y ret u rn nothing u sef u l E x ception : barplot() f u nction DATA VISUALIZATION IN R
Side effects and ret u rn v al u es 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") DATA VISUALIZATION IN R
s y mbols () sho w s relations bet w een 3 or more v ariables 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") DATA VISUALIZATION IN R
Sa v ing 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") DATA VISUALIZATION IN R
Let ' s practice ! DATA VISU AL IZATION IN R
Using color effecti v el y DATA VISU AL IZATION IN R Ron Pearson Instr u ctor
Limitations of color Color - blindness : not e v er y one can see colors Black - and -w hite reprod u ction loses all color - coded details Can be o v er u sed and lose u sef u lness DATA VISUALIZATION IN R
Iliinsk y & Steele ' s recommended colors " Ideall y, abo u t si x ..." "... hopef u ll y no more than 12 ..." "... and absol u tel y no more than 20" DATA VISUALIZATION IN R
Iliinsk y & Steele ' s recommended colors DATA VISUALIZATION IN R
Iliinsk y & Steele ' s recommended colors DATA VISUALIZATION IN R
Iliinsk y & Steele ' s recommended colors DATA VISUALIZATION IN R
Let ' s practice ! DATA VISU AL IZATION IN R
Other graphics s y stems in R DATA VISU AL IZATION IN R Ron Pearson Instr u ctor
Wh y base R ? Fle x ible Good for e x plorator y anal y sis Eas y to learn DATA VISUALIZATION IN R
The grid graphics s y stem Based on the grid package Greater control o v er lo w- le v el graphical details More � e x ible than base graphics Comes at cost of steep learning c u r v e 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) DATA VISUALIZATION IN R
gridBase Req u ires familiarit y w ith both graphics s y stems DATA VISUALIZATION IN R
The lattice graphics s y stem B u ilt on grid graphics Ver y good for conditional graphs DATA VISUALIZATION IN R
Ho w does mpg v s horsepo w er v ar y b y c y linders ? library(MASS) library(lattice) xyplot(MPG.city ~ Horsepower | Cylinders, data = Cars93) DATA VISUALIZATION IN R
The ggplot 2 graphics package Ver y pop u lar graphics package based on grid graphics The bases for other DataCamp co u rses Allo w s u s to b u ild comple x plots in stages DATA VISUALIZATION IN R
E x ample w ith ggplot 2 # 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) DATA VISUALIZATION IN R
Let ' s practice ! DATA VISU AL IZATION IN R
Recommend
More recommend