What is R?

new-talk

new-talk

For more details on authoring R presentations

https://support.rstudio.com/hc/en-us/articles/200486468.

https://github.com/tidyverse/ggplot2/wiki/Why-use-ggplot2

Step 1 R Base install

https://cran.r-project.org/bin/windows/base/

new-talk

new-talk

Step 2 Download and Ticket IT will help and install Rstudios Desktop

new-talk1

new-talk1

https://www.rstudio.com/products/rstudio/download/

Step 3 Self-study R

install.packages("swirl")
library("swirl")
# Làm theo hướng dẫn mở swirl()
library("swirl")
## Warning: package 'swirl' was built under R version 3.4.3
## 
## | Hi! Type swirl() when you are ready to begin.

Step 3: Các bước tiến hành trên Rstudios

Giao diện new-talk

Vào File > New > tạo R script new-talk

install.packages("swirl")
library("swirl")
# Làm theo hướng dẫn mở swirl()
library("swirl")

Step 4: Làm quen với việc lưu trữ, viết script và bỏ file vào thư mục

new-talk

new-talk

getwd()
setwd("G:/Team Drives/RStudios Learning Sharing/Num1")

VD 1

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.3
library(scales)
## Warning: package 'scales' was built under R version 3.4.3
library(reshape2)
## Warning: package 'reshape2' was built under R version 3.4.3
data(tips)

dat <- data.frame(
  time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(14.89, 17.23)
)
# No1: The idea is that you can piece together various parts using the grammar for other visualization types. Whereas the single function call to barplot() is specialized to one thing.
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
  geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") +
  guides(fill=FALSE) +
  xlab("Time of day") + ylab("Total bill") +
  ggtitle("Average bill for 2 people")

par(las=1)
barplot(dat$total_bill,
        names.arg=dat$time,
        col="#AFC0CB",
        border=FALSE,
        main="Average Bill for Two-Person Meal")

VD 2

# No2: Again, notice the component approach for ggplot2 with calls to geom_point() and geom_line(). In contrast, I use a call to plot() to make a line chart and then points() to add the circles at the end of the line.
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +
  geom_line(colour="red", linetype="dashed", size=1.5) +
  geom_point(colour="red", size=4, shape=21, fill="white")

plot(c(1,2), dat$total_bill, type="l", xlab="time", ylab="",
    lty=2, lwd=3, col="red")
points(c(1,2), dat$total_bill, pch=21, col="red", cex=2,
    bg="white", lwd=3)

VD 3 ========================================================

# No3: legend: mutiple variance

dat1 <- data.frame(
  sex = factor(c("Female","Female","Male","Male")),
  time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(13.53, 16.81, 16.24, 17.42)
)

ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(stat="identity", position=position_dodge(),
      colour="black") +
  scale_fill_manual(values=c("#999999", "#E69F00"))

dat1mat <- matrix( dat1$total_bill,
                   nrow = 2,
                   byrow=TRUE,
                   dimnames = list(c("Female", "Male"), c("Lunch", "Dinner"))
                   )

mf_col <- c("#3CC3BD", "#FD8210")
barplot(dat1mat, beside = TRUE, border=NA, col=mf_col)
legend("topleft", row.names(dat1mat), pch=15, col=mf_col)

VD 4 ========================================================

#No4: default color
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
  geom_bar(colour="black", stat="identity",
           position=position_dodge(),
           size=.3) +                       
  scale_fill_hue(name="Sex of payer") +     
  xlab("Time of day") + ylab("Total bill") +
  ggtitle("Average bill for 2 people") +    
  theme_bw() +
  coord_cartesian(ylim = c(12,18))

par(cex=1.2, cex.axis=1.1)
barplot(dat1mat, beside = TRUE, border=NA, col=mf_col,
    main="Average Bill for Two People", yaxt="n")
axis(2, at=axTicks(2), labels=sprintf("$%s", axTicks(2)),
    las=1, cex.axis=0.8)
grid(NA, NULL, lwd=1, lty=1, col="#ffffff")
abline(0, 0)
text(1.5, dat1mat["Female", "Lunch"], "Female", pos=3)
text(2.5, dat1mat["Male", "Lunch"], "Male", pos=3)
text(1.5, dat1mat["Female", "Lunch"]+0.7, "SEX OF PAYER",
    pos=3, cex=0.75)

VD 5 ========================================================

# tuong tu
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex,
    shape=sex, colour=sex)) +
  geom_line(aes(linetype=sex), size=1) +    
  geom_point(size=3, fill="white") +        
  expand_limits(y=0) +                      
  scale_colour_hue(name="Sex of payer", l=30) +
  scale_shape_manual(name="Sex of payer", values=c(22,21)) +
  scale_linetype_discrete(name="Sex of payer") +
  xlab("Time of day") + ylab("Total bill") +
  ggtitle("Average bill for 2 people") +    
  theme_bw() +
  theme(legend.position=c(.7, .4))

par(cex=1.2, cex.axis=1.1)
matplot(dat1mat, type="b", lty=1, pch=19, col=mf_col,
    cex=1.5, lwd=3, las=1, bty="n", xaxt="n",
    xlim=c(0.7, 2.2), ylim=c(12,18), ylab="",
    main="Average Bill for Two People", yaxt="n")
axis(2, at=axTicks(2), labels=sprintf("$%s", axTicks(2)),
    las=1, cex.axis=0.8, col=NA, line = -0.5)
grid(NA, NULL, lty=3, lwd=1, col="#000000")
abline(v=c(1,2), lty=3, lwd=1, col="#000000")
mtext("Lunch", side=1, at=1)
mtext("Dinner", side=1, at=2)
text(1.5, 17.3, "Male", srt=8, font=3)
text(1.5, 15.1, "Female", srt=33, font=3)

Facet_grid with GGPLOT2
========================================================

#Facet grid
data(tips)
sp <- ggplot(tips,aes(x=total_bill, y=tip/total_bill))
sp + geom_point()

sp + facet_grid(. ~ sex) + geom_point()

With Base ========================================================

par(mfrow=c(1,2))
sexes <- unique(tips$sex)
for (i in 1:length(sexes)) {
  currdata <- tips[tips$sex == sexes[i],]
  plot(currdata$total_bill, currdata$tip/currdata$total_bill,
    main=sexes[i], ylim=c(0,0.7))
}

Facet_grid with GGPLOT2
========================================================

#Facet grid
sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill))
sp    + geom_point(shape =1)

sp + facet_grid(sex ~ day)+ geom_point(shape =1)

Facet_grid with GGPLOT2
========================================================

#Facet grid
sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill))
sp    + geom_point(shape =1)

sp + facet_grid(sex ~ day)+ geom_point(shape =1)

Facet_grid With Base ========================================================

par(mfrow=c(2,4))
days <- c("Thur", "Fri", "Sat", "Sun")
sexes <- unique(tips$sex)
for (i in 1:length(sexes)) {
  for (j in 1:length(days)) {
    currdata <- tips[tips$day == days[j] & tips$sex == sexes[i],]
    plot(currdata$total_bill, currdata$tip/currdata$total_bill,
        main=paste(days[j], sexes[i], sep=", "), ylim=c(0,0.7), las=1)
  }
}

Facet_grid With Base

par(mfrow=c(2,4))
days <- c("Thur", "Fri", "Sat", "Sun")
sexes <- unique(tips$sex)
for (i in 1:length(sexes)) {
  for (j in 1:length(days)) {
    currdata <- tips[tips$day == days[j] & tips$sex == sexes[i],]
    plot(currdata$total_bill, currdata$tip/currdata$total_bill,
        main=paste(days[j], sexes[i], sep=", "), ylim=c(0,0.7), las=1)
  }
}

Down Audience 6.csv, và Audience v2 6.csv từ Team drive vào getwd():

https://drive.google.com/drive/folders/0AAGr9J_23BVCUk9PVA

Other example A

library(Rcmdr)
## Warning: package 'Rcmdr' was built under R version 3.4.3
## Loading required package: splines
## Loading required package: RcmdrMisc
## Warning: package 'RcmdrMisc' was built under R version 3.4.3
## Loading required package: car
## Warning: package 'car' was built under R version 3.4.3
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 3.4.3
## Loading required package: effects
## Warning: package 'effects' was built under R version 3.4.3
## Loading required package: carData
## 
## Attaching package: 'carData'
## The following objects are masked from 'package:car':
## 
##     Guyer, UN, Vocab
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## The Commander GUI is launched only in interactive sessions
library(scales)
library(GGally)
## Warning: package 'GGally' was built under R version 3.4.3
library(ggplot2)
library(reshape2)
library(magrittr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:GGally':
## 
##     nasa
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(varhandle)
library(car)
library(plyr)
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
library(lubridate)
## Warning: package 'lubridate' was built under R version 3.4.3
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:plyr':
## 
##     here
## The following object is masked from 'package:base':
## 
##     date
library(rgl)
## Warning: package 'rgl' was built under R version 3.4.3
Audience <- read.csv("Audience 6.csv")
Audience2 <- read.csv("Audience v2 6.csv")

Audience$Rev30<- gsub(",", "", as.character(factor(Audience$Rev30)))
Audience$Rev30 <- as.numeric(Audience$Rev30)

Audience$Rev90<- gsub(",", "", as.character(factor(Audience$Rev90)))
Audience$Rev90 <- as.numeric(Audience$Rev90)

Audience$RR1 <- gsub(",", ".", as.character(factor(Audience$RR1)))
Audience$RR1 <- gsub("%", "", as.character(factor(Audience$RR1)))
Audience$RR1 <- as.numeric(Audience$RR1)

Audience$RR7 <- gsub(",", ".", as.character(factor(Audience$RR7)))
Audience$RR7 <- gsub("%", "", as.character(factor(Audience$RR7)))
Audience$RR7 <- as.numeric(Audience$RR7)


Audience$Imp<- gsub(",", "", as.character(factor(Audience$Imp)))
Audience$Imp <- as.numeric(Audience$Imp)

Audience$Click<- gsub(",", "", as.character(factor(Audience$Click)))
Audience$Click <- as.numeric(Audience$Click)

Audience$Install..GMT.7.<- gsub(",", "", as.character(factor(Audience$Install..GMT.7.)))
Audience$Install..GMT.7. <- as.numeric(Audience$Install..GMT.7.)

Audience$NRU<- gsub(",", "", as.character(factor(Audience$NRU)))
Audience$NRU <- as.numeric(Audience$NRU)

Audience$NRU0<- gsub(",", "", as.character(factor(Audience$NRU0)))
Audience$NRU0 <- as.numeric(Audience$NRU0)


arm <- Audience

arm$CTR <- NULL
arm$CR <- NULL
arm$CPC <- NULL
arm$NRU0.Install <- NULL
arm$CR7 <- NULL
arm$CR30 <- NULL
arm$Rev7.CostVND <- NULL
arm$Rev30.CostVND <- NULL
arm$Rev90.CostVND <- NULL
arm$CTR <- NULL

#
arm$RR1 <- NULL
arm$RR7 <- NULL
arm$Rev7 <- NULL
arm$Rev30 <- NULL
arm$Rev90 <- NULL
arm$Cost... <- NULL

arm$ï..Date <- as.Date(arm$ï..Date)
arm$ï..Date <- as.Date(arm$ï..Date, format="%m/%d/%Y")

At6 <- subset(arm,arm$ï..Date >='2017-06-01' & arm$ï..Date<= '2017-06-30')
At7 <- subset(arm,arm$ï..Date >='2017-07-01' & arm$ï..Date<= '2017-07-31')
At8 <- subset(arm,arm$ï..Date >='2017-08-01' & arm$ï..Date<= '2017-08-30')

#Bao quat
arm$ï..Date <- NULL

arm1 <- ggpairs(arm,
                upper = list(continuous = wrap("density", alpha = 0.5), combo = "box"),
                lower = list(continuous = wrap("points", alpha = 0.3,    size=0.1), 
                             combo = wrap("dot", alpha = 0.4, size=0.2)),cardinality_threshold = NULL)
arm1

B

AudienceIns7 <- ggplot(At7) + 
  aes(x= Install..GMT.7., y= Audience) + 
  geom_point(aes(color = Device.Os),alpha = 0.4,position = "jitter")+
  ggtitle("Tháng 7")

AudienceIns7

AudienceIns8 <- ggplot(At8) + 
  aes(x= Install..GMT.7., y= Audience) + 
  geom_point(aes(color = Device.Os),alpha = 0.4,position = "jitter")+
  ggtitle("Tháng 8")
AudienceIns8

C

arm1 <- Audience 

arm1$ï..Date <- as.Date(arm1$ï..Date)
arm1$ï..Date <- as.Date(arm1$ï..Date, format="%m/%d/%Y")

AT6 <- subset(arm1,arm1$ï..Date >='2017-06-01' & arm1$ï..Date<= '2017-06-30')
AT7 <- subset(arm1,arm1$ï..Date >='2017-07-01' & arm1$ï..Date<= '2017-07-31')
AT8 <- subset(arm1,arm1$ï..Date >='2017-08-01' & arm1$ï..Date<= '2017-08-30')

rrrr8 <- ggplot(AT8) + 
  aes(x= RR1, y= RR7) + 
  geom_point(aes(color = Audience, shape = Source),alpha = 0.4,position = "jitter")+
  theme(axis.text.x  = element_text(angle = 60,hjust = 1))+
  coord_cartesian(xlim = c(0,100),ylim = c(0,80))+
  ggtitle("Tháng 8")
rrrr8

rrrr8 <- ggplot(AT8) + 
  aes(x= RR1, y= RR7) + 
  geom_point(aes(color = Audience, shape = Source),alpha = 0.4,position = "jitter")+
  theme(axis.text.x  = element_text(angle = 60,hjust = 1))+
  coord_cartesian(xlim =c(25,75) ,ylim = c(10,30))+
  ggtitle("Tháng 8")
rrrr8

D

rrrr7 <- ggplot(AT7) + 
  aes(x= RR1, y= RR7) + 
  geom_point(aes(color = Audience),alpha = 0.4,position = "jitter")+
  theme(axis.text.x  = element_text(angle = 60,hjust = 1))+
  coord_cartesian(xlim =c(0,100),ylim = c(0,80))+
  ggtitle("Tháng 7")
rrrr7

rrrr7 <- ggplot(AT7) + 
  aes(x= RR1, y= RR7) + 
  geom_point(aes(color = Audience),alpha = 0.4,position = "jitter")+
  theme(axis.text.x  = element_text(angle = 60,hjust = 1))+
  coord_cartesian(xlim = c(25,75) ,ylim = c(10,30))+
  ggtitle("Tháng 7")
rrrr7

HISTOGRAM

data=data.frame(value=rnorm(10000))
# Uniform color
ggplot(data, aes(x=value)) + 
    geom_histogram(binwidth = 0.2, color="white", fill=rgb(0.2,0.7,0.1,0.4) ) 

# Proportional color
ggplot(data, aes(x=value)) + 
    geom_histogram(binwidth = 0.2, aes(fill = ..count..) )

COLOR

# create a dataset
specie=c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition=rep(c("normal" , "stress" , "Nitrogen") , 4)
value=abs(rnorm(12 , 0 , 15))

data=data.frame(specie,condition,value)
# color with RcolorBrewer
ggplot(data, aes(fill=condition, y=value, x=specie)) +
    geom_bar( stat="identity", position="fill") +    
    scale_fill_brewer(palette = "Set1")

# Faceting
ggplot(data, aes(y=value, x=specie, color=specie, fill=specie)) +
    geom_bar( stat="identity") +    
    facet_wrap(~condition)

TEXT LABEL ========================================================

# The mtcars dataset is proposed in R
data=head(mtcars, 30)
 
# 1/ add text with geom_text, use nudge to nudge the text
ggplot(data, aes(x=wt, y=mpg)) +
  geom_point() + 
  geom_text(label=rownames(data), nudge_x = 0.25, nudge_y = 0.25, check_overlap = T)

# 2/ to improve readability, use geom_label
ggplot(data, aes(x=wt, y=mpg)) +
  geom_point() + 
  geom_label(label=rownames(data), nudge_x = 0.25, nudge_y = 0.2)

# 3/ custom geom_label like any other geom.
ggplot(data, aes(x=wt, y=mpg, fill=cyl)) +
  geom_label(label=rownames(data), color="white", size=5)

BOXPLOT

# The mtcars dataset is proposed in R
data(mtcars)
# geom_boxplot proposes several arguments to custom appearance
ggplot(mpg, aes(x=reorder(class, hwy), y=hwy, fill=class)) + 
    geom_boxplot() +
    xlab("class") +
    theme(legend.position="none")

# Data 
names=c(rep("A", 80) , rep("B", 50) , rep("C", 70))
value=c( sample(2:5, 80 , replace=T) , sample(4:10, 50 , replace=T), 
       sample(1:7, 70 , replace=T) )
data=data.frame(names,value)
 
#Graph
qplot( x=names , y=value , data=data , geom=c("boxplot","jitter") , fill=names)

CONNECTION MAP ========================================================

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.4.3
## -- Attaching packages -------------------------------------- tidyverse 1.2.1 --
## v tibble  1.3.4     v purrr   0.2.4
## v tidyr   0.7.2     v stringr 1.2.0
## v readr   1.1.1     v forcats 0.2.0
## Warning: package 'tidyr' was built under R version 3.4.3
## Warning: package 'readr' was built under R version 3.4.3
## Warning: package 'stringr' was built under R version 3.4.3
## Warning: package 'forcats' was built under R version 3.4.3
## -- Conflicts ----------------------------------------- tidyverse_conflicts() --
## x plyr::arrange()          masks dplyr::arrange()
## x lubridate::as.difftime() masks base::as.difftime()
## x readr::col_factor()      masks scales::col_factor()
## x purrr::compact()         masks plyr::compact()
## x plyr::count()            masks dplyr::count()
## x lubridate::date()        masks base::date()
## x purrr::discard()         masks scales::discard()
## x tidyr::extract()         masks magrittr::extract()
## x plyr::failwith()         masks dplyr::failwith()
## x dplyr::filter()          masks stats::filter()
## x lubridate::here()        masks plyr::here()
## x plyr::id()               masks dplyr::id()
## x lubridate::intersect()   masks base::intersect()
## x dplyr::lag()             masks stats::lag()
## x plyr::mutate()           masks dplyr::mutate()
## x dplyr::recode()          masks car::recode()
## x plyr::rename()           masks dplyr::rename()
## x purrr::set_names()       masks magrittr::set_names()
## x lubridate::setdiff()     masks base::setdiff()
## x purrr::some()            masks car::some()
## x plyr::summarise()        masks dplyr::summarise()
## x plyr::summarize()        masks dplyr::summarize()
## x lubridate::union()       masks base::union()
library(maps)
## Warning: package 'maps' was built under R version 3.4.3
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
## The following object is masked from 'package:plyr':
## 
##     ozone
library(geosphere)
## Warning: package 'geosphere' was built under R version 3.4.3
library(ggplot2)
par(mar=c(0,0,0,0))
map('world',col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05,mar=rep(0,4),border=0, ylim=c(-80,80) )

plot_my_connection=function( dep_lon, dep_lat, arr_lon, arr_lat, ...){
    inter <- gcIntermediate(c(dep_lon, dep_lat), c(arr_lon, arr_lat), n=50, addStartEnd=TRUE, breakAtDateLine=F)             
    inter=data.frame(inter)
    diff_of_lon=abs(dep_lon) + abs(arr_lon)
    if(diff_of_lon > 180){
        lines(subset(inter, lon>=0), ...)
        lines(subset(inter, lon<0), ...)
    }else{
        lines(inter, ...)
        }
    }
 

data=rbind(
    Buenos_aires=c(-58,-34),
    Paris=c(2,49),
    Melbourne=c(145,-38),
    Saint.Petersburg=c(30.32, 59.93),
    Abidjan=c(-4.03, 5.33),
    Montreal=c(-73.57, 45.52),
    Nairobi=c(36.82, -1.29),
    Salvador=c(-38.5, -12.97)
    )  %>% as.data.frame()
colnames(data)=c("long","lat")

all_pairs=cbind(t(combn(data$long, 2)), t(combn(data$lat, 2))) %>% as.data.frame()
colnames(all_pairs)=c("long1","long2","lat1","lat2")

# background map
par(mar=c(0,0,0,0))
map('world',col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05,mar=rep(0,4),border=0, ylim=c(-80,80) )
 
# add every connections:
for(i in 1:nrow(all_pairs)){
    plot_my_connection(all_pairs$long1[i], all_pairs$lat1[i], all_pairs$long2[i], all_pairs$lat2[i], col="skyblue", lwd=1)
    }
 
# add points and names of cities
points(x=data$long, y=data$lat, col="slateblue", cex=2, pch=20)
text(rownames(data), x=data$long, y=data$lat,  col="slateblue", cex=1, pos=4)

DENSITY

library(rattle)
## Rattle: A free graphical interface for data science with R.
## Version 5.1.0 Copyright (c) 2006-2017 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
weatherAUS <- read.csv("weatherAUS.csv")

# To generate a density plot.
 
cities <- c("Canberra", "Darwin", "Melbourne", "Sydney")
ds <- subset(weatherAUS, Location %in% cities & ! is.na(Temp3pm))
p  <- ggplot(ds, aes(Temp3pm, colour=Location, fill=Location))
p  <- p + geom_density(alpha=0.55)
p

MARGINAL HISTOGRAM FOR GGPLOT2

library(ggExtra)
## Warning: package 'ggExtra' was built under R version 3.4.3
# The mtcars dataset is proposed in R
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
# classic plot :
p=ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) +
      geom_point() +
      theme(legend.position="none")
 
# with marginal histogram
ggMarginal(p, type="histogram")
 
# marginal boxplot
ggMarginal(p, type="boxplot")

ANNOTATION

p=ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
 
# Add text on a specific positions:
p + annotate("text", x = c(2,4.5), y = c(20,25), label = c("label 1", "label 2") , color="orange", size=5 , angle=45, fontface="bold")

SPYWEB ========================================================

# Library
library(fmsb)
 
# Create data: note in High school for Jonathan:
data=as.data.frame(matrix( sample( 2:20 , 10 , replace=T) , ncol=10))
colnames(data)=c("math" , "english" , "biology" , "music" , "R-coding", "data-viz" , "french" , "physic", "statistic", "sport" )
 
# To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each topic to show on the plot!
data=rbind(rep(20,10) , rep(0,10) , data)
 
# The default radar chart proposed by the library:
radarchart(data)

# Custom the radarChart !
radarchart( data  , axistype=1 ,
 
    #custom polygon
    pcol=rgb(0.2,0.5,0.5,0.9) , pfcol=rgb(0.2,0.5,0.5,0.5) , plwd=4 ,
 
    #custom the grid
    cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,20,5), cglwd=0.8,
 
    #custom labels
    vlcex=0.8
    )

RADAR 1 ========================================================

# Library
library(fmsb)
 
# Create data: note in High school for several students
set.seed(99)
data=as.data.frame(matrix( sample( 0:20 , 15 , replace=F) , ncol=5))
colnames(data)=c("math" , "english" , "biology" , "music" , "R-coding" )
rownames(data)=paste("mister" , letters[1:3] , sep="-")
 
# To use the fmsb package, I have to add 2 lines to the dataframe: the max and min of each topic to show on the plot!
data=rbind(rep(20,5) , rep(0,5) , data)
 
 
 
 
 
#==================
# Plot 1: Default radar chart proposed by the library:
radarchart(data)

#==================
# Plot 2: Same plot with custom features
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data  , axistype=1 ,
    #custom polygon
    pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
    #custom the grid
    cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,20,5), cglwd=0.8,
    #custom labels
    vlcex=0.8
    )
legend(x=0.7, y=1, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)

RADAR 2 ========================================================

# Plot3: If you remove the 2 first lines, the function compute the max and min of each variable with the available data:
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data[-c(1,2),]  , axistype=0 , maxmin=F,
    #custom polygon
    pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1,
    #custom the grid
    cglcol="grey", cglty=1, axislabcol="black", cglwd=0.8,
    #custom labels
    vlcex=0.8
    )
legend(x=0.7, y=1, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "grey", cex=1.2, pt.cex=3)

CIRCULAR BARPLOT

# make data
data=data.frame(group=c("A ","B ","C ","D ") , value=c(33,62,56,67) )

# Usual bar plot :
ggplot(data, aes(x = group, y = value ,fill = group )) + 
  geom_bar(width = 0.85, stat="identity")

# Circular one
ggplot(data, aes(x = group, y = value ,fill = group)) + 
  geom_bar(width = 0.85, stat="identity") +    
  
  # To use a polar plot and not a basic barplot
  coord_polar(theta = "y") +    
  
  #Remove useless labels of axis
  xlab("") + ylab("") +
  
  #Increase ylim to avoid having a complete circle
  ylim(c(0,75)) + 
  
  #Add group labels close to the bars :
  geom_text(data = data, hjust = 1, size = 3, aes(x = group, y = 0, label = group)) +
  
  #Remove useless legend, y axis ticks and y axis text
  theme(legend.position = "none" , axis.text.y = element_blank() , axis.ticks = element_blank())

STACK PLOT

# library
library(ggplot2)
 
# create a dataset
specie=c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition=rep(c("normal" , "stress" , "Nitrogen") , 4)
value=abs(rnorm(12 , 0 , 15))
data=data.frame(specie,condition,value)
 
# Grouped
ggplot(data, aes(fill=condition, y=value, x=specie)) +
    geom_bar(position="dodge", stat="identity")

# Stacked
ggplot(data, aes(fill=condition, y=value, x=specie)) +
    geom_bar( stat="identity")

# Stacked Percent
ggplot(data, aes(fill=condition, y=value, x=specie)) +
    geom_bar( stat="identity", position="fill")

TREEMAP

# library
library(treemap)
## Warning: package 'treemap' was built under R version 3.4.3
# Create data
group=c(rep("group-1",4),rep("group-2",2),rep("group-3",3))
subgroup=paste("subgroup" , c(1,2,3,4,1,2,1,2,3), sep="-")
value=c(13,5,22,12,11,7,3,1,23)
data=data.frame(group,subgroup,value)
 
# Custom labels:
treemap(data, index=c("group","subgroup"),     vSize="value", type="index",
 
    fontsize.labels=c(15,12),                # size of labels. Give the size per level of aggregation: size for group, size for subgroup, sub-subgroups...
    fontcolor.labels=c("white","orange"),    # Color of labels
    fontface.labels=c(2,1),                  # Font of labels: 1,2,3,4 for normal, bold, italic, bold-italic...
    bg.labels=c("transparent"),              # Background color of labels
    align.labels=list(
        c("center", "center"), 
        c("right", "bottom")
        ),                                   # Where to place labels in the rectangle?
    overlap.labels=0.5,                      # number between 0 and 1 that determines the tolerance of the overlap between labels. 0 means that labels of lower levels are not printed if higher level labels overlap, 1  means that labels are always printed. In-between values, for instance the default value .5, means that lower level labels are printed if other labels do not overlap with more than .5  times their area size.
    inflate.labels=F,                        # If true, labels are bigger when rectangle is bigger.
 
)

TREEMAP ========================================================

# Custom borders:
treemap(data, index=c("group","subgroup"), vSize="value", type="index",
 
    border.col=c("black","white"),             # Color of borders of groups, of subgroups, of subsubgroups ....
    border.lwds=c(7,2)                         # Width of colors
        
    )

TREEMAP ========================================================

# Custom borders:
treemap(data, index=c("group","subgroup"), vSize="value", 
 
    type="index",                            # How you color the treemap. type help(treemap) for more info
    palette = "Set1",                        # Select your color palette from the RColorBrewer presets or make your own.
    title="My Treemap",                      # Customize your title
    fontsize.title=12,                       # Size of the title
 
)     

Slide With Plotly

library(plotly)
## Warning: package 'plotly' was built under R version 3.4.3
## 
## Attaching package: 'plotly'
## The following objects are masked from 'package:plyr':
## 
##     arrange, mutate, rename, summarise
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
plotlyy<- ggplot(mpg, aes(x=reorder(class, hwy), y=hwy, fill=class)) + 
    geom_boxplot() +
    xlab("class") +
    theme(legend.position="none")
 ggplotly(plotlyy)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
# Tất cả chart tĩnh vẽ từ ggplot sẽ trở thành chart động thông qua ggplotly, hãy chạm chuột thử phóng vào.

Advantages

Disadvantages

Art

library(gsubfn)
## Warning: package 'gsubfn' was built under R version 3.4.3
## Loading required package: proto
## Warning: package 'proto' was built under R version 3.4.3
library(stringr)
library(dplyr)
library(ggplot2)
 
#Plant 1
axiom="F"
rules=list("F"="FF-[-F+F+F]+[+F-F-F]")
angle=22.5
depth=4
 
#Plant 2
axiom="X"
rules=list("X"="F[+X][-X]FX", "F"="FF")
angle=25.7
depth=7
 
#Plant 3
axiom="X"
rules=list("X"="F[+X]F[-X]+X", "F"="FF")
angle=20
depth=7
 
#Plant 4
axiom="X"
rules=list("X"="F-[[X]+X]+F[+FX]-X", "F"="FF")
angle=22.5
depth=5

for (i in 1:depth) axiom=gsubfn(".", rules, axiom)
 
actions=str_extract_all(axiom, "\\d*\\+|\\d*\\-|F|L|R|\\[|\\]|\\|") %>% unlist
 
status=data.frame(x=numeric(0), y=numeric(0), alfa=numeric(0))
points=data.frame(x1 = 0, y1 = 0, x2 = NA, y2 = NA, alfa=90, depth=1)
 
 
for (action in actions) 
{
  if (action=="F")
  {
    x=points[1, "x1"]+cos(points[1, "alfa"]*(pi/180))
    y=points[1, "y1"]+sin(points[1, "alfa"]*(pi/180))
    points[1,"x2"]=x
    points[1,"y2"]=y
    data.frame(x1 = x, y1 = y, x2 = NA, y2 = NA, 
               alfa=points[1, "alfa"],
               depth=points[1,"depth"]) %>% rbind(points)->points
  }
  if (action %in% c("+", "-")){
    alfa=points[1, "alfa"]
    points[1, "alfa"]=eval(parse(text=paste0("alfa",action, angle)))
  }
  if(action=="["){ 
    data.frame(x=points[1, "x1"], y=points[1, "y1"], alfa=points[1, "alfa"]) %>% 
      rbind(status) -> status
    points[1, "depth"]=points[1, "depth"]+1
  }
   
  if(action=="]"){ 
    depth=points[1, "depth"]
    points[-1,]->points
    data.frame(x1=status[1, "x"], y1=status[1, "y"], x2=NA, y2=NA, 
               alfa=status[1, "alfa"],
               depth=depth-1) %>% 
      rbind(points) -> points
    status[-1,]->status
  }
}
 
ggplot() + 
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2), 
               lineend = "round", 
               colour="white",
               data=na.omit(points)) + 
  coord_fixed(ratio = 1) +
  theme(legend.position="none",
        panel.background = element_rect(fill="black"),
        panel.grid=element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank(),
        axis.text=element_blank())

#Plant 6
axiom="F"
rules=list("F"="F[+F]F[-F][F]")
angle=20
depth=5
 
 
for (i in 1:depth) axiom=gsubfn(".", rules, axiom)
 
actions=str_extract_all(axiom, "\\d*\\+|\\d*\\-|F|L|R|\\[|\\]|\\|") %>% unlist
 
status=data.frame(x=numeric(0), y=numeric(0), alfa=numeric(0))
points=data.frame(x1 = 0, y1 = 0, x2 = NA, y2 = NA, alfa=90, depth=1)
 
 
for (action in actions) 
{
  if (action=="F")
  {
    x=points[1, "x1"]+cos(points[1, "alfa"]*(pi/180))
    y=points[1, "y1"]+sin(points[1, "alfa"]*(pi/180))
    points[1,"x2"]=x
    points[1,"y2"]=y
    data.frame(x1 = x, y1 = y, x2 = NA, y2 = NA, 
               alfa=points[1, "alfa"],
               depth=points[1,"depth"]) %>% rbind(points)->points
  }
  if (action %in% c("+", "-")){
    alfa=points[1, "alfa"]
    points[1, "alfa"]=eval(parse(text=paste0("alfa",action, angle)))
  }
  if(action=="["){ 
    data.frame(x=points[1, "x1"], y=points[1, "y1"], alfa=points[1, "alfa"]) %>% 
      rbind(status) -> status
    points[1, "depth"]=points[1, "depth"]+1
  }
   
  if(action=="]"){ 
    depth=points[1, "depth"]
    points[-1,]->points
    data.frame(x1=status[1, "x"], y1=status[1, "y"], x2=NA, y2=NA, 
               alfa=status[1, "alfa"],
               depth=depth-1) %>% 
      rbind(points) -> points
    status[-1,]->status
  }
}
 
ggplot() + 
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2), 
               lineend = "round", 
               colour="white",
               data=na.omit(points)) + 
  coord_fixed(ratio = 1) +
  theme(legend.position="none",
        panel.background = element_rect(fill="black"),
        panel.grid=element_blank(),
        axis.ticks=element_blank(),
        axis.title=element_blank(),
        axis.text=element_blank())