creating and sa v ing more comple x plots
play

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 ,


  1. Creating and sa v ing more comple x plots DATA VISU AL IZATION IN R Ron Pearson Instr u ctor

  2. 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

  3. 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

  4. 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

  5. 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

  6. Let ' s practice ! DATA VISU AL IZATION IN R

  7. Using color effecti v el y DATA VISU AL IZATION IN R Ron Pearson Instr u ctor

  8. 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

  9. 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

  10. Iliinsk y & Steele ' s recommended colors DATA VISUALIZATION IN R

  11. Iliinsk y & Steele ' s recommended colors DATA VISUALIZATION IN R

  12. Iliinsk y & Steele ' s recommended colors DATA VISUALIZATION IN R

  13. Let ' s practice ! DATA VISU AL IZATION IN R

  14. Other graphics s y stems in R DATA VISU AL IZATION IN R Ron Pearson Instr u ctor

  15. Wh y base R ? Fle x ible Good for e x plorator y anal y sis Eas y to learn DATA VISUALIZATION IN R

  16. 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

  17. 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

  18. gridBase Req u ires familiarit y w ith both graphics s y stems DATA VISUALIZATION IN R

  19. The lattice graphics s y stem B u ilt on grid graphics Ver y good for conditional graphs DATA VISUALIZATION IN R

  20. 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

  21. 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

  22. 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

  23. Let ' s practice ! DATA VISU AL IZATION IN R

Recommend


More recommend