Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SOC 4930 & SOC 5050 - Week 02, Lecture 02b

SOC 4930 & SOC 5050 - Week 02, Lecture 02b

Lecture slides for Week 02, Lecture 02b of the Saint Louis University Course Quantitative Analysis: Applied Inferential Statistics. These slides cover the basics of the R package ggplot2.

Christopher Prener

September 04, 2017
Tweet

More Decks by Christopher Prener

Other Decks in Education

Transcript

  1. AGENDA QUANTITATIVE ANALYSIS / WEEK 02 / LECTURE 02B 1.

    Statistical Transformations 2. Aesthetic Adjustments 3. Coordinate Systems 4. The Grammar of Graphics
  2. ggplot2::ggplot(data = dataFrame) + geom_bar(mapping = aes(aesthetic)) Example - the

    mpg data from ggplot2: ggplot(data = mpg) + geom_bar(mapping = aes(class)) 1. STATISTICAL TRANSFORMATIONS BAR PLOTS
  3. 1. STATISTICAL TRANSFORMATIONS BAR PLOTS ggplot2::ggplot(data = dataFrame) + geom_bar(mapping

    = aes(aesthetic), stat = “identity”) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_bar(mapping = aes(class), stat = “identity”) The stat “identity” is implied by default when you use the bar geom
  4. 1. STATISTICAL TRANSFORMATIONS AREA PLOTS ggplot2::ggplot(data = dataFrame) + geom_area(mapping

    = aes(aesthetic), stat = “bin”) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_area(mapping = aes(hwy), stat = “bin”) This geom is for use with one continuous variable, stat must be included
  5. 1. STATISTICAL TRANSFORMATIONS SUMMARIZING VALUES Example - the mpg data

    from ggplot2: ggplot(data = mpg) + stat_summary( mapping = aes(x = class, y = hwy), fun.ymin = min, fun.ymax = max, fun.y = median )
  6. ggplot2::ggplot(data = dataFrame) + geom_point(mapping = aes(x = var1, y

    = var2), color = “color”) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = “blue”) You cannot use the fill option instead of color with this geom 2. AESTHETIC ADJUSTMENTS ADDING COLOR ARBITRARILY
  7. ggplot2::ggplot(data = dataFrame) + geom_point(mapping = aes(x = var1, y

    = var2), color = “color”) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = “blue”) You cannot use the fill option instead of color with this geom For now, avoid setting colors arbitrarily and let ggplot2 do the work instead! 2. AESTHETIC ADJUSTMENTS ADDING COLOR ARBITRARILY
  8. ggplot2::ggplot(data = dataFrame) + geom_point(mapping = aes(x = var1, y

    = var2, color = “color”)) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = “class”)) You cannot use the fill option instead of color with this geom 2. AESTHETIC ADJUSTMENTS ADDING COLOR TO THE AESTHETIC
  9. ggplot2::ggplot(data = dataFrame) + geom_point(mapping = aes(x = var1, y

    = var2, color = var3)) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class)) You cannot use the fill option instead of color here 2. AESTHETIC ADJUSTMENTS ADDING COLOR BASED ON OTHER VALUES
  10. ggplot2::ggplot(data = dataFrame) + geom_bar(mapping = aes(x = var, fill

    = var)) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_bar(mapping = aes(x = class, fill = class)) This can also be done with the color option, but fill is preferable 2. AESTHETIC ADJUSTMENTS ADDING COLOR BASED ON OTHER VALUES
  11. ggplot2::ggplot(data = dataFrame) + geom_bar(mapping = aes(x = var, fill

    = var)) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_bar(mapping = aes(x = class, fill = trans)) 2. AESTHETIC ADJUSTMENTS ADDING COLOR BASED ON OTHER VALUES
  12. ggplot2::ggplot(data = dataFrame) + geom_bar(mapping = aes(x = var, fill

    = var), position = “dodge”) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_bar(mapping = aes(x = class, fill = trans), position = “dodge”) 2. AESTHETIC ADJUSTMENTS POSITION ADJUSTMENTS - DODGE
  13. ggplot2::ggplot(data = dataFrame) + geom_point(mapping = aes(x = var1, y

    = var2), position = “jitter”) Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), position = “jitter”) 2. AESTHETIC ADJUSTMENTS POSITION ADJUSTMENTS - JITTER
  14. 3. COORDINATE SYSTEMS ▸ French/Dutch philosopher and mathematician ▸ Published

    the idea of data arrayed along an axes in 1647 ▸ Has been transformed into what we now understand to be Cartesian coordinate systems ▸ A number of other thinkers wrote about similar ideas independently in the same period RENÉ DESCARTES
  15. 3. COORDINATE SYSTEMS FLIPPING COORDINATES ggplot2::ggplot(data = dataFrame) + geom_bar(mapping

    = aes(x = var, fill = var) + coord_flip() Example - the mpg data from ggplot2: ggplot(data = mpg) + geom_bar(mapping = aes(x = class, fill = trans), position = “dodge”) + coord_flip()
  16. 4. THE GRAMMAR OF GRAPHICS A BASIC TEMPLATE ggplot2::ggplot(data =

    dataFrame) + geom(mapping = aes(aesthetics), stat = statistics, position = position, ) + coordinateFunction + facetFunction