Wednesday, 11 January 2017

Introduction to ggplot

Lets color the R-World


Data for reference : Iris

Colouring and comparing sepal vs petal
qplot(Sepal.Length, Petal.Length, data = iris, color = Species)

You need to have the package ggplot installed  to proceed further:

install.packages("ggplot2")

#Adding the color parameter (here we have species)

qplot(Sepal.Length, Petal.Length, data = iris, color = Species)

#Adding another dimension in the size parameter
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width)


# Adding the x and y axes and giving the title
qplot(Sepal.Length, Petal.Length, data = iris, color = Species,
      xlab = "Sepal Length", ylab = "Petal Length", 
      main = "Sepal vs. Petal Length in Sushovan's Iris data")



#Instead of dots or points, let's try line using geom parameter
qplot(Sepal.Length, Petal.Length, data = iris, geom = "line", color = Species)



#And now..... geom as bar though it does not make any sense here
  qplot(Species, data = iris, weight = Sepal.Length, geom = "bar", ylab = "Sepal.Length")


#Lets see the geom as bar in a user defined dataframe
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)
)

# Here the height of each bar is the total running time of the director's movies.
qplot(director, weight = minutes, data = movies, geom = "bar", ylab = "total length (min.)")



# `Orange` is another built-in data frame that describes the growth of orange trees.
qplot(age, circumference, data = Orange, geom = "line",
      colour = Tree,
      main = "How does orange tree circumference vary with age?")



R-File for reference : Iris R

That's it for today......Enjoy coloring the world!! :)






No comments:

Post a Comment