Statistical Graphics
- R language provides excellent graphing capabilities, bot in the base installation and also with add-on packages such as
lattice
andggplot2
- 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
- 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
- Lets create a histogram showing distribution of carat
- 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
- other ways of scatter plot generation
- Box Plots:
- 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
- Lets try to drwa the density plot
- Lets draw the scatter plot
- ggplot2 has ability to make faceted plots or small multiples
- Lets try some more stuff
- Line Graphs: Lets start using economics dataset from ggplot2
- 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
- lets consider only data from year 1967
- A great part of ggplot2 is the ability to change themes. There are themes in package
ggthemes