Data Science Classroom Series – 19/Dec/2021

Statistical Graphics

  • R language provides excellent graphing capabilities, bot in the base installation and also with add-on packages such as lattice and ggplot2
  • Base Graphics:
    • We need some data to plot graphs and Most of the datasets built into R are tiny.
    • We need a dataset which is part of ggplot2 installation Preview
    • Base Histograms: The most common graph of data in a single variable is a histogram which show distribution of values fora variable
      • Lets create a histogram showing distribution of carat Preview
    • Base Scatter Plot: It is frequently good to see two variables in comparision with each other, that is where the scatter plot is use
      • To draw the scatter plot, we provide the data and two variables to compare
      • plot(price ~ carat, data=diamonds)
      • The ~ seperating price and carat indicates that we are viewing price against carat from the diamond data set Preview
      • other ways of scatter plot generation Preview
    • Box Plots: Preview
  • ggplot2:
    • Initially the ggplot2 syntax is kind of harder to grasp, but the effort is worthwile, It is much easier to delineate data by color, shape, size or add legends with ggplot2.
    • The basic structure for ggplot2 starts with ggplot function which is at is mos basic that should take the data as its first agrument.
    • After intializing this object we add layers using the + symbol
    • To star we will be using geometric layers such as plots, lines and histograms which are included in functions like geom_point, geom_line and geom_histogram
    • Draw an Histogram Preview
    • Lets try to drwa the density plot Preview
    • Lets draw the scatter plot Preview Preview
    • ggplot2 has ability to make faceted plots or small multiples Preview Preview
    • Lets try some more stuff Preview
    • Line Graphs: Lets start using economics dataset from ggplot2 Preview Preview
    • Generally when we are plotting, we might be expected to display the graphs year by year
    • For this we would be using a package lubridate Preview
    • lets consider only data from year 1967 Preview
    • A great part of ggplot2 is the ability to change themes. There are themes in package ggthemes Preview Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin