R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. My data are in Excel which has extention .xlsx and to read it properly I need a library(readxl) and I also need library (ggplot2) as I shall be using ggplot2 library by Hadley Wickham for data visualization. ggplot2 is one very powerful tool in R for data visualization and is basic tool which every data scientist must know.

library(readxl)
## Warning: package 'readxl' was built under R version 3.4.2
Potato<-read_xlsx("Potato1.xlsx",col_names = TRUE)
library(ggplot2)

Including Plots

You can also embed plots, for example:

Since this graph has no title and to add title we have used ggtitle (“Monthly Potato Prices 2008-15”)

ggplot(data = Potato,aes(x=Date,y=price))+geom_line()+ggtitle("Monthly Potato Prices 2008-15")

##To adjust line width we give argument in geom_line
p1<-ggplot(data = Potato,aes(x=Date,y=price))+geom_line(size=1)+ggtitle("Monthly Potato Prices 2008-15")
p1

## To adjust x-axis
##White Theme
p1+theme_bw()

str(Potato)
## Classes 'tbl_df', 'tbl' and 'data.frame':    85 obs. of  2 variables:
##  $ Date : POSIXct, format: "2008-07-01" "2008-08-01" ...
##  $ price: num  37.2 38 39.5 39 43.5 ...

The graph has nice grey background

We can edit this graph in many ways and one of the objective of this exercise is to use various themes (Used by some well known magzines of the world : The Economist, Wall Street Journal etc). For this we need some more packages to be installed and these are given below.

library(ggthemes)
## Warning: package 'ggthemes' was built under R version 3.4.2
library(readr)
## Warning: package 'readr' was built under R version 3.4.2

Now we shall use the theme given in The Economist

p1+theme_economist()

Another very useful theme often used is that of fivethirtyeight.com

p1+theme_fivethirtyeight()