1.A little demonstration of pipe function
library(tidyr)
data(iris)
iris %>% summary()
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
plot(diff(log(sample(rnorm(n = 10000,mean = 10,sd = 1),size = 100,replace = FALSE))),type = "b",col="red")
library(ggplot2)
qplot(x = cty, y = hwy, color = cyl, data = mpg, geom = "point",main = "mpg data")
ggplot(data = mpg,mapping=aes(x = cty,y = hwy))+
geom_point(aes(color=cyl))+
coord_cartesian()+
scale_color_gradient()+
theme_bw()
single variable
a<-ggplot(data = mpg,aes(hwy))
a+geom_area(stat = "bin",binwidth=10)
a+geom_density(kernel="gaussian")
a+geom_dotplot()
## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
a+geom_freqpoly(binwidth=10)
a+geom_bar(color="red",fill="green",size=0.1)
2 variable
b<-ggplot(data = mpg,mapping=aes(x = cty,y = hwy))
b+geom_jitter(color="blue2",size=4,shape="S")+geom_jitter(mapping=aes(cty,displ),color="red3")#new layer
b+geom_point(color="brown2")
b+geom_quantile(color="blue2",size=0.3)
## Loading required package: SparseM
##
## Attaching package: 'SparseM'
## The following object is masked from 'package:base':
##
## backsolve
## Smoothing formula not specified. Using: y ~ x
b+geom_smooth(method = loess)
b+geom_rug(sides = "rb")#right left for y; bottom top for x
b+geom_text(mapping=aes(label=cty))
3 other layer, mutiple plots
c <- ggplot(data = mpg,aes(cty,hwy))
c+geom_bin2d(binwidth=c(0.5,1))#heat map
c+geom_density2d(color="red3")
c+geom_point(color="green3")+facet_grid(.~fl)#divide by fl into column
c+geom_point(color="green3")+facet_grid(fl~.)#divide by fl into row
c+geom_point(color="green3")+facet_grid(year~fl)#divide by year in row and divide by fl in column
c+geom_point(color="green3")+facet_wrap(~fl)
require(ggplot2)
data(diamonds)
set.seed(42)
small <- diamonds[sample(nrow(diamonds), 1000), ]
head(small)
## Source: local data frame [6 x 10]
##
## carat cut color clarity depth table price x y z
## (dbl) (fctr) (fctr) (fctr) (dbl) (dbl) (int) (dbl) (dbl) (dbl)
## 1 0.71 Very Good H SI1 62.5 60 2096 5.68 5.75 3.57
## 2 0.79 Premium H SI1 61.8 59 2275 5.97 5.91 3.67
## 3 1.03 Ideal F SI1 62.4 57 6178 6.48 6.44 4.03
## 4 0.50 Ideal E VS2 62.2 54 1624 5.08 5.11 3.17
## 5 0.27 Ideal E VS1 61.6 56 470 4.14 4.17 2.56
## 6 0.30 Premium E VS2 61.7 58 658 4.32 4.34 2.67
ggplot(data = small,mapping = aes(x = carat,y = price,color="darkblue",shape=cut))+geom_point()
ggplot(data = small)+geom_point(mapping = aes(x = carat,y=price,color=color,shape=cut))
ggplot(data=small)+geom_histogram(mapping = aes(x = price,fill=cut))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=small)+geom_histogram(mapping = aes(x = price,fill=cut),position = "dodge")#dodge prevents overlap
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(small)+geom_bar(mapping = aes(clarity,fill=cut))
ggplot(small)+geom_density(mapping = aes(x = price,color=cut))
ggplot(small)+geom_density(mapping = aes(x = price,fill=cut))
ggplot(small)+geom_boxplot(aes(x = cut,y = price,fill=color))
ggplot(small)+
geom_point(aes(x=carat, y=price, shape=cut, color=color))+
scale_y_log10()+
scale_color_discrete(h=c(150,350), c=100, l=60)#sedu,qiangdu,mingdu
ggplot(small,mapping = aes(x = carat,y = price,color=cut))+
geom_point() +
scale_color_manual(values=c('blue','cyan', 'yellow', 'orange', 'red'))+
theme_light()
ggplot(small, aes(x=carat, y=price))+
geom_point(color=alpha("darkblue",0.2))+
scale_y_log10()+
stat_smooth()#common reflection to ggplot
ggplot(small)+geom_bar(aes(x=cut, fill=cut))+coord_flip()# flip the coordinate
#basically a pie chart
ggplot(small)+geom_bar(aes(x=factor(1), fill=cut))+coord_polar(theta = "y")#angle theta is mapped to the y(cut size) #x is the radias which is constant as one
ggplot(small)+geom_bar(aes(x=clarity, fill=cut))+coord_polar()