Introduction
ggplot is the grammar of graphics
created by Hadley Wickham. It has significantly reduced the difficulty
of creating plots. We create plots or graphics to understand the
characteristics of the features that we’re going to use in modelling or
simple understanding for business scenario. This post is created for
people who are new to ggplot but have knowledge of R. To create the
plots, we are going to use the data of iris setosa classification
scenario.
Understanding qplot
qplot is used when we want to create a
plot without much tweaking of graphics. We leave the graphics to the
ggplot library’s default settings. And it gives more colourful plots
when we compare with the default plot that we get in R.
Scatter Plot
# Plotting the Sepal length vs Petal Length of the iris dataset.
qplot(Sepal.Length, Petal.Length, data = iris)
# Plotting the same as above but also adding a new dimension.
qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
# Adding one more dimension of size of the bubble or the point.
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width)
# Adding a little bit more transparency effect to avoid showing the overlapping. Alpha has to be added to include the transparency parameter.
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7))
Now we will Create Our own Dataset to visualise and learn different Features of ggplot.
movies = data.frame(
director = c("spielberg", "spielberg", "spielberg", "jackson", "jackson"),
movie = c("jaws", "avatar", "schindler's list", "lotr", "king kong"),
minutes = c(124, 163, 195, 600, 187))
qplot(director, data = movies, geom = "bar", ylab = "movies", color = movie)
qplot(director, weight = minutes, data = movies, geom = "bar", ylab = "total length (min.)")
qplot(Sepal.Length, Petal.Length, data = iris, geom = "line", color = Species)
qplot(age, circumference, data = Orange, geom = "line",
colour = Tree, main = "How does orange tree circumference vary with age?")
# We can also plot both points and lines.
qplot(age, circumference, data = Orange, geom = c("point", "line"), colour = Tree)
No comments:
Post a Comment