当年学习R语言也是经历了很多挫折的,不知道如何导入Excel数据,丑陋的R原生UI界面,可是后来我发现,学习一门编程语言或者一门技术吧,首先要建立起兴趣来,兴趣是最好的老师,所以首先我没有急着用R去完成什么任务,而是选了一本作图的书培养兴趣,每次调用ggplot2照猫画虎做出了和书上一样赏心悦目的图,总能使我感受到成功的乐趣,逐渐的由点带面,建立起了对R的信任以及对R强大功能的震惊和深深的折服,同时要感谢Rstudio带给我的一站式解决问题的环境,使我沉浸在R的世界里。等兴趣培养起来了,然后就要找一本系统全面专业权威的书了,这里首推《R语言实战》,当然《R语言数据科学》也非常棒,只是当时还没有出版,但是里面的内容其实我基本上通过帮助系统和包自带的手册也学了个80%。最后要感谢的是工作,我为了能解放自己,只好去奴役计算机了,期间我编写了FTP收益率曲线自动生成后导入到Excel里,那叫一个酣畅淋漓啊,后来一发不可收拾好,资金头寸系统每月报告涉及的数据也能自动完成了,再后来遇到了Rmarkdown,写报告那叫一个完美了!说了很多,其实就是想告诉你如果你也想学习R语言,不妨从作图开始。

这篇文章可能永远不会终结,我会时不时把我看到的酷炫的图更新进来,一来备忘,二来安利以下R,希望有更多的人喜欢R,如果R确实帮助到你了,那我要恭喜你了!科技解放人力,希望你继续努力学习R,争取让你的人生不在那么苦逼,自勉吧!另外我还想告诉你,在人工智能即将全面爆发之前,人类最后一次站在了食物链的最顶端,要想让自己站在最顶端的最顶端,那我告诉你,或许可以考虑学金融!如果你不知道干什么,建议你学一下量化金融。说的有点多了,咱们还是从作图开始吧!Needless to say, talk is cheap, let’s show you the code!

概率密度图

1
2
3
4
5
6
7
8
9
data(iris)
attach(iris)
plot(Petal.Length, rep(-0.03,length(Species)), xlim=c(1,7),
     ylim=c(0,1.7), xlab="Petal.Length", ylab="Density",
     pch=21, cex=1.5, col="#00000001", main = "Iris (yet again)",
     bg=c("#ff000020","#00ff0020","#0000ff20")[unclass(Species)])
for (i in 1:3)
  polygon(density(Petal.Length[unclass(Species)==i],bw=0.2),
          col=c("#ff000040","#00ff0040","#0000ff40")[i])

一只蝴蝶

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
t=seq(-400, 4, by=0.01)
a=cos(t)
b=cos(4*t)
x=sin(t)*(exp(a)-2*b-(sin(t/12))*sin(t/12)*sin(t/12)*sin(t/12)*sin(t/12))
y = cos(t)*(exp(a)-2*b-(sin(t/12)*sin(t/12)*sin(t/12)*sin(t/12)*sin(t/12)))
x1=seq(0.20, (pi/4+0.20), by=0.01)
y1=3*sin(2*(x1-0.20))+2.5;
x2=-1*x1;

plot(x, y, xlim=c(-4,4), ylim=c(-3,6),col=4, lty=2, pch=3, cex=0.7)
points(x1,y1, col=2, pch="*", cex=1.5)
points(x2,y1, col=2, pch="*", cex=1.5)

相关系数矩阵图

1
2
3
library(corrplot)
M = cor(mtcars)
corrplot.mixed(M, upper = "ellipse", lower = "circle")

马赛克图

1
2
library(vcd)
mosaic(~Class+Sex+Age+Survived, data=Titanic, shade=TRUE, legend=TRUE)

和弦图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
library(statnet)
library(circlize)

mydata <- matrix(ncol=4,
  c( 105, 20, 118, 100,450, 46, 553, 18,
     92, 78, 94, 108,96, 33, 317, 104))

rownames(mydata) <- c("ID.1", "ID.2", "ID.3", "ID.4")
colnames(mydata) <- c("Chr.1", "Chr.2", "Chr.3", "Chr.4")

grid.col = NULL
grid.col[c("ID.1", "ID.2", "ID.3", "ID.4")] = c("red", "yellow","green", "blue")
grid.col[c("Chr.1", "Chr.2", "Chr.3", "Chr.4")] = "grey"

circos.par(gap.degree = c(rep(2, nrow(mydata)-1), 10, rep(2, ncol(mydata)-1), 10),
           start.degree = 180)
chordDiagram(mydata, 
             directional = TRUE,
             diffHeight = 0.06,
             grid.col = grid.col, 
             transparency = 0.5)

1
circos.clear()

山脊图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
library(tidyverse)
library(ggridges)
library(viridis)

windowsFonts('yh' = '微软雅黑')
ggplot(iris) +
  geom_density_ridges_gradient(
    aes(x = Sepal.Length, y = Species, fill = ..x..,height = ..density..), 
    scale = 1, rel_min_height = 0.01) +
  theme_ridges(font_size = 13, grid = TRUE) +
  theme(
    text = element_text(family = 'yh'),
    legend.position = 'right'
  ) +
  scale_y_discrete(name = '', expand = c(0.01, 0)) +
  scale_x_continuous(name = '', expand = c(0.01, 0)) +
  scale_fill_viridis(name = "", option = "C") 

局部加权回归的散点图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## load data
library(SpikeIn) ##to install: rafalib::install_bioc("SpikeIn")
data(SpikeIn95) 

## get M and A for two arrays
spms <- pm(SpikeIn95)
i <- 10; j  <- 9 ## Pick two samples that show a non-linear relationship
M <- log2(spms[,i]) - log2(spms[,j])
A <- (log2(spms[,i]) + log2(spms[,j]))/2
o <- order(A)
a <- A[o]
m <- M[o]
ind <- round(seq(1,length(a),len=5000)) ##don't show all the points
a <- a[ind]
m <- m[ind]

## make gif
centers <- seq(min(a),max(a),0.1)
windowSize <- 1.5
smooth <- rep(NA,length(centers))
library(animation)
makeplot <- function(){
  for(i in seq(along=centers))
    {
    center <- centers[i]
    ind=which(a > center-windowSize & a<center+windowSize)
    fit<-lm(m~a, subset=ind)
    smooth[i]<-predict(fit, newdata=data.frame(a=center))
    if(center<12){
      plot(a, m, ylim=c(-1.5,1.5), col="grey")
      points(a[ind], m[ind])
      abline(fit, col=3, lty=2, lwd=2)
      lines(centers[1:i], smooth[1:i], col=2, lwd=2)
      points(centers[i], smooth[i], col=2, pch=16)
    }
  }
}
saveGIF(makeplot(), interval = 0.15,
        "~/static/post/data_visualization_files/figure-html/animation.gif")