Ggplot2 is an advance R library designed for creating charts. Ggplot2 is one of the 3 graphics system in R, other than base and lattice. it was written by Hadley Wickham.
Web site: http://ggplot2.org
GGplot2 Overview
GGplot2 is like a R grammar system for graphics where the syntax consists various parts of graphics and you can build graphs using this syntax provided by ggplot2. Its like mapping various attributes of graphs in R, it addresses various components of graphs and stitch them together to a build complete chart. These are important elements of Graphs in ggplot2 in R;
· Data
· Aesthetics like shape, size, and color
· Geometic objects like points, lines etc
Broadly it is similar to other graphics systems in R, it will look for the data in data frame or environment and graphs are made up of aesthetics (shape,size, color) and geoms (points, lines). GGPlot2 consist for 2 graph functions and they are;
- ggplot
- qplot
ggplot vs qplot
As mentioned above, these are two ggplot2 functions which are used to create graphs in R using ggplot2 graph system. The utilization of either of them depends upon the complexity & requirement of the project.
· ggplot is used for larger complex data
· qplot which is used for simpler data sets
ggplot() is the core function and it is used when qplot is not sufficient whereas qplot is used when you are not looking at too much of functionality.
Like other R libraries, you first need to install ggplot2 library
After installing, Initiate ggplot2 in R studio
Code:
1 2 3 4 5 |
> library(ggplot2) Warning message: package ‘ggplot2’ was built under R version 3.2.2 |
let’s use the in-built dataset for qplot example;
initiate mpg dataframe in R studio by typing;
1 |
str(mpg) |
which will be followed by this;
1 2 3 4 5 6 7 8 9 10 11 12 |
'data.frame': 234 obs. of 11 variables: manufacturer: Factor w/ 15 levels "audi","chevrolet",..: 1 1 1 1 1 1 1 1 1 1 ... model : Factor w/ 38 levels "4runner 4wd",..: 2 2 2 2 2 2 2 3 3 3 ... displ : num 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ... year : int 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ... cyl : int 4 4 4 4 6 6 6 4 4 4 ... trans : Factor w/ 10 levels "auto(av)","auto(l3)",..: 4 9 10 1 4 9 1 9 4 10 ... drv : Factor w/ 3 levels "4","f","r": 2 2 2 2 2 2 2 1 1 1 ... cty : int 18 21 20 21 16 18 18 18 16 20 ... hwy : int 29 29 31 30 26 26 27 26 25 28 ... fl : Factor w/ 5 levels "c","d","e","p",..: 4 4 4 4 4 4 4 4 4 4 ... class : Factor w/ 7 levels "2seater","compact",..: 2 2 2 2 2 2 2 2 2 2 ... |
Not type following command in R studio;
1 |
qplot(displ, hwy, data=mpg) |
- where displ is the x coordinate
- hwy is the y coordinate
- mpg is the data frame
you can replace these variables in your graph to get desired result. Above code will generate following graph;
ggplot function
ggplot accepts and need following elements in the command;
• A data frame
• aestheic: details of data mapped to aesthetics like color, size
• geoms: points, lines, shapes.
• facets: in case of conditional plots.
• stats: quantiles, smoothing.
• scales: male = red, female = blue etc
• coordinate system
Let’s use the above mentioned data set for ggplot example, in this case we will first create objects for each activity and then use them to print graph;
1 2 3 4 5 |
> g <- ggplot(mpg,aes(displ,hwy)) > g Error: No layers in plot > pp<- g + geom_point() > pp |
It will generate following graph;
In upcoming post, we will see more graphs using ggplot2 and other graphics system in R. If you have any question or suggestion, please leave your comment.
Speak Your Mind