data_vis

Introduction to data Visualization in R

data structure Nilanjan Chatterjee
February, 2020

Topics

What is data visualization?

Technique to communicate insights from data through visual representation.
Allow easy understanding of large dataset.
Provides basic knowledge about variables.
Most efficient way to identify, locate, manipulate, format, and present data.

Why data visualization is important?

How to do data visualization?

data(mtcars)
plot(mpg~wt, mtcars, pch=19, col="blue")

plot of chunk unnamed-chunk-2

plot vs ggplot

plot vs ggplot

Pros Cons
In-built Additional package
Easy to learn Steep learning curve
Indepenedent of data-structures Works only with data-frame
Easy for simple plots Verbose for complex plots
Low level of abstraction High abstraction level
Visually less appealing Visually more appealing

ggplot

plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3

ggplot

Based on Grammer of graphics (Wilkinson, 2005).
Consists of several building blocks like a sentence.

lost or very-lost

ggplot

#install.packages("ggplot2", dependencies = T)
library(ggplot2)
ggplot(mtcars, aes(x= wt, y= mpg))+ 
  geom_point(colour="blue", size=3) 

plot of chunk unnamed-chunk-5

yay

How to plot in ggplot

ggplot(mtcars) #data

plot of chunk unnamed-chunk-7

How to plot in ggplot

ggplot(mtcars, aes(x= wt, y= mpg)) #data+aesthetic map

plot of chunk unnamed-chunk-8

How to plot in ggplot

ggplot(mtcars, aes(x= wt, y= mpg))+ #data+aesthetic map
  geom_point() #geometric obj

plot of chunk unnamed-chunk-9

How to plot in ggplot

ggplot(mtcars, aes(x= wt, y= mpg))+ #data+aesthetic map
  geom_point(colour="blue", size=3) #geometric obj

plot of chunk unnamed-chunk-10

How to plot in ggplot

ggplot(mtcars, aes(x= wt, y= mpg))+ #data+aesthetic map
  geom_point(colour="blue", size=3)+ #geometric obj
  ggtitle("Scatterplot") #Plot title

plot of chunk unnamed-chunk-11

Different sections of ggplot

Some more examples

ggplot(mtcars, aes(x=mpg))+
  geom_bar()

plot of chunk unnamed-chunk-12

Some more examples

ggplot(mtcars, aes(x=cyl, y=mpg, fill= cyl))+ 
  geom_bar(stat="identity")

plot of chunk unnamed-chunk-13

Some more examples

ggplot(mtcars, aes(x=cyl, y=mpg))+ 
  geom_point(stat="identity", size=4)

plot of chunk unnamed-chunk-14

Export graphs from R/Rstudio

You can export any plots using the plot window from R/RStudio.
To save files in high-resolution these commands are helpful

sct <-ggplot(mtcars, aes(x= wt, y= mpg))+ 
  geom_point(colour="blue", size=3)+   ggtitle("Scatterplot")

ggsave(sct, "Scatterplot_with_R.jpeg", dpi=100, device = "jpeg")

Exercise

Thanks

thank-you