| ## title: “Data visualization with ggplot2” ** |
| author: “Vikash R. Satyal” |
| date: “3/29/2020” |
| output: pdf_document |
{r setup, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
dmtcars<-data.frame(mtcars)
#install.packages("ggplot2")
library(ggplot2)
# generates a blank sheet
d<-ggplot(data=mtcars, aes(x=wt, y=mpg))
d
#fill the blank sheet
d+geom_point() #scatter diagram
# change point color by value of 3ed variable
d+geom_point(aes(color=cyl))
d+geom_point(aes(color=cyl))+scale_color_gradientn(colours = rainbow(5))
#Add point identifying text
d+geom_point()+geom_text(label=rownames(mtcars), size=2.5)
## Warning: package 'car' was built under R version 3.6.3
## Loading required package: carData
# adding line of best fit & LOWESS fit
d+geom_point()+geom_smooth(method=lm, levels=90)
## Warning: Ignoring unknown parameters: levels
d+geom_point()+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#change plot point size by 3rd variable
ggplot(mtcars, aes(x=wt, y=mpg, size=disp)) +
geom_point(shape=21, color="black", fill="blue")
#Facet plot for Male & Female
ggplot(Salaries, aes(x=yrs.since.phd, y=salary, color=rank,
shape=rank)) + geom_point() + facet_grid(.~sex)
# type of stack bar & Position of graph label
m<-ggplot(Salaries, aes(x=rank, fill=sex))
m+geom_bar(position="stack") + labs(title='position="stack"')
m+geom_bar(position="dodge") + labs(title='position="dodge"')
m+geom_bar(position="fill") + labs(title='position="fill"')
#bar plots of data diamond
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, color = cut))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut))
#time series plot
t <- ggplot(data = economics, aes(x = date, y = unemploy))
t <- t + geom_line()
t
t1 <- t + geom_smooth(method = loess, span=1)
t1
data(singer, package="lattice")
ggplot(singer, aes(x=height))+ geom_histogram()+geom_density(colour="black", adjust=4)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# PLOTS
ggplot(singer, aes(x=voice.part, y=height)) + geom_boxplot()
boxplot(mpg~ gear*am, data=mtcars)
#install.packages("car")
library(car)
ggplot(data=Salaries, aes(x=salary, fill=rank)) +
geom_density(alpha=.3)
#multiple grapgs in one page
g1<-ggplot(data=Salaries, aes(x=rank))+geom_bar()+coord_flip()
g2<-ggplot(data=Salaries, aes(x=sex))+geom_bar()
g3<-ggplot(data=Salaries,aes(x=yrs.since.phd,y=salary))+geom_point()
g4<-ggplot(data=Salaries,aes(x=rank,y=salary))+
geom_boxplot()
library(gridExtra)
grid<-grid.arrange(g1,g2,g3,g4,top=("Grid plot of salaries"))
grid
## TableGrob (3 x 2) "arrange": 5 grobs
## z cells name grob
## 1 1 (2-2,1-1) arrange gtable[layout]
## 2 2 (2-2,2-2) arrange gtable[layout]
## 3 3 (3-3,1-1) arrange gtable[layout]
## 4 4 (3-3,2-2) arrange gtable[layout]
## 5 5 (1-1,1-2) arrange text[GRID.text.1608]
#Saving the previous graph
ggsave("mygrid.pdf", grid, width = 4, height = 5)
# is saved in my 'document' folder
```r
# One ellipse arround all points
ggplot(faithful, aes(waiting, eruptions))+
geom_point()+
stat_ellipse()
# Ellipse by groups
p <- ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3))+
geom_point()
p + stat_ellipse()
# Change the type of ellipses: possible values are "t", "norm", "euclid"
p + stat_ellipse(type = "norm")