作者:带土;Daitu; Adam 邮箱:

在程序开头设置所有绘图风格

5.1 igraph包可视化

社交网络图,邻接矩阵可视化

## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
##     Country airportnumber vtype
## 1   Algeria            43     2
## 2 Australia           296     3
## 3   Bahamas            33     2
## 4   Belgium            24     2
## 5    Brazil           234     3
## 6    Canada           417     3
##   Country.y     Country.x connectnumber etype
## 1   Algeria        France            67     1
## 2 Australia   New Zealand            64     1
## 3   Bahamas United States            72     1
## 4   Belgium         Spain            60     1
## 5    Brazil United States            57     1
## 6    Canada United States           364     3
## 从数据表中定义网络图
g <- graph_from_data_frame(edge, ## 边的数据
                           vertices = vertex, ## 节点数据
                           directed = FALSE ## 表示图像为无向图
                           )

## 对得到的图像进行进一步的修整
## 根据数据中的其它变量,定义边的粗细,为添加边的粗细特征
E(g)$width <- floor(log10(E(g)$connectnumber))
##  定义图像中会用到的颜色和形状向量,节点和边的颜色
colrs <- c("gray", "red", "blue")
shape <- c( "circle","csquare","sphere")
## 为图中的节点定义颜色,alpha.f为设置颜色的透明程度
V(g)$color <- adjustcolor(colrs[V(g)$vtype],alpha.f = 0.5)
## 为图中的边定义颜色
E(g)$color <- colrs[E(g)$etype]
# plot 4个图 - 2 rows, 2 columns,每个图使用不同的图像样式
par(mfrow=c(2,2), mar=c(0,0,1,0),family = "STKaiti") 
plot(g, 
     ## 使用圆形布局
     layout =  layout_in_circle(g),
     ## 节点的大小和形状
     vertex.size = floor(10*log10(V(g)$airportnumber)),
     vertex.shape = shape[V(g)$vtype],
     ## 节点标签的缩放比例
     vertex.label.cex = 0.6,vertex.label.color = "yellow",
     main = "圆形布局")
plot(g, layout =  layout_with_fr(g),
     ## 节点的大小和形状
     vertex.size = floor(10*log10(V(g)$airportnumber)),
     vertex.shape = shape[V(g)$vtype],
     ## 节点标签的缩放比例
     vertex.label.cex = 0.6,vertex.label.color = "yellow",
     main = "弹力模型布局")

plot(g, layout =  layout_on_sphere(g),
     ## 节点的大小和形状
     vertex.size = floor(10*log10(V(g)$airportnumber)),
     vertex.shape = shape[V(g)$vtype],
     ## 节点标签的缩放比例
     vertex.label.cex = 0.6,vertex.label.color = "yellow",
     main = "球体表面布局")
plot(g, layout =  layout_randomly(g),
     ## 节点的大小和形状
     vertex.size = floor(10*log10(V(g)$airportnumber)),
     vertex.shape = shape[V(g)$vtype],
     ## 节点标签的缩放比例
     vertex.label.cex = 0.6,vertex.label.color = "yellow",
     main = "随机布局")

## 6 x 34 sparse Matrix of class "dgCMatrix"
##    [[ suppressing 34 column names 'Mr Hi', 'Actor 2', 'Actor 3' ... ]]
##                                                                            
## Mr Hi   . 4 5 3 3 3 3 2 2 . 2 3 1 3 . . . 2 . 2 . 2 . . . . . . . . . 2 . .
## Actor 2 4 . 6 3 . . . 4 . . . . . 5 . . . 1 . 2 . 2 . . . . . . . . 2 . . .
## Actor 3 5 6 . 3 . . . 4 5 1 . . . 3 . . . . . . . . . . . . . 2 2 . . . 2 .
## Actor 4 3 3 3 . . . . 3 . . . . 3 3 . . . . . . . . . . . . . . . . . . . .
## Actor 5 3 . . . . . 2 . . . 3 . . . . . . . . . . . . . . . . . . . . . . .
## Actor 6 3 . . . . . 5 . . . 3 . . . . . 3 . . . . . . . . . . . . . . . . .

## 34 x 34 sparse Matrix of class "dgCMatrix"
##    [[ suppressing 34 column names 'Mr Hi', 'Actor 2', 'Actor 3' ... ]]
##                                                                             
## Mr Hi    . 1 1 1 1 1 1 1 1 . 1 1 1 1 . . . 1 . 1 . 1 . . . . . . . . . 1 . .
## Actor 2  1 . 1 1 . . . 1 . . . . . 1 . . . 1 . 1 . 1 . . . . . . . . 1 . . .
## Actor 3  1 1 . 1 . . . 1 1 1 . . . 1 . . . . . . . . . . . . . 1 1 . . . 1 .
## Actor 4  1 1 1 . . . . 1 . . . . 1 1 . . . . . . . . . . . . . . . . . . . .
## Actor 5  1 . . . . . 1 . . . 1 . . . . . . . . . . . . . . . . . . . . . . .
## Actor 6  1 . . . . . 1 . . . 1 . . . . . 1 . . . . . . . . . . . . . . . . .
## Actor 7  1 . . . 1 1 . . . . . . . . . . 1 . . . . . . . . . . . . . . . . .
## Actor 8  1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
## Actor 9  1 . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . 1 1
## Actor 10 . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
## Actor 11 1 . . . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
## Actor 12 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
## Actor 13 1 . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
## Actor 14 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
## Actor 15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1
## Actor 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1
## Actor 17 . . . . . 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . .
## Actor 18 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
## Actor 19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1
## Actor 20 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
## Actor 21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1
## Actor 22 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
## Actor 23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1
## Actor 24 . . . . . . . . . . . . . . . . . . . . . . . . . 1 . 1 . 1 . . 1 1
## Actor 25 . . . . . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . . 1 . .
## Actor 26 . . . . . . . . . . . . . . . . . . . . . . . 1 1 . . . . . . 1 . .
## Actor 27 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . 1
## Actor 28 . . 1 . . . . . . . . . . . . . . . . . . . . 1 1 . . . . . . . . 1
## Actor 29 . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . 1
## Actor 30 . . . . . . . . . . . . . . . . . . . . . . . 1 . . 1 . . . . . 1 1
## Actor 31 . 1 . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . 1 1
## Actor 32 1 . . . . . . . . . . . . . . . . . . . . . . . 1 1 . . 1 . . . 1 1
## Actor 33 . . 1 . . . . . 1 . . . . . 1 1 . . 1 . 1 . 1 1 . . . . . 1 1 1 . 1
## John A   . . . . . . . . 1 1 . . . 1 1 1 . . 1 1 1 . 1 1 . . 1 1 1 1 1 1 1 .

5.2 地图数据可视化

无坐标数据的地图数据可视化

## Loading required package: sp
## ### Welcome to rworldmap ###
## For a short introduction type :   vignette('rworldmap')
##   iso3          country     continent population    GNI
## 3  BMU          Bermuda North America      67837 106140
## 4  NOR           Norway        Europe    4676305 103630
## 5  QAT            Qatar          Asia     833285  92200
## 6  CHE      Switzerland        Europe    7604467  88120
## 7  MAC Macao SAR, China          Asia     559846  76270
## 8  LUX       Luxembourg        Europe     491775  75990
##   iso3          country     continent population    GNI  log2GNI
## 3  BMU          Bermuda North America      67837 106140 16.69561
## 4  NOR           Norway        Europe    4676305 103630 16.66108
## 5  QAT            Qatar          Asia     833285  92200 16.49248
## 6  CHE      Switzerland        Europe    7604467  88120 16.42718
## 7  MAC Macao SAR, China          Asia     559846  76270 16.21883
## 8  LUX       Luxembourg        Europe     491775  75990 16.21352
## 184 codes from your data successfully matched countries in the map
## 4 codes from your data failed to match with a country code in the map
## 59 codes from the map weren't represented in your data
## 184 codes from your data successfully matched countries in the map
## 4 codes from your data failed to match with a country code in the map
## 59 codes from the map weren't represented in your data

5.3 pheatmap包可视化热力图

详细介绍pheatmap包对数据进行可视化和可视化相关系数的包

## corrplot 0.84 loaded
##                Raw        Soak   Sonicate  Cellulase     Steam  Sonsteam
## sample1  1.3921110  0.70391718  0.5831566  0.5599809 0.3163506 0.5672277
## sample2  0.8527846 -0.43475544  0.5831566  0.4206362 0.3163506 0.5672277
## sample3  0.8527846  0.64561536 -1.7423985 -0.1312379 0.3163506 0.5672277
## sample4 -0.9271987 -0.58854932  0.5831566  0.4206362 0.3163506 0.5672277
## sample5 -0.3488467 -3.01213477  0.5831566  0.4206362 0.3818094 0.4333861
## sample6 -0.4101890 -0.02442258  1.4620044  1.7227105 0.3163506 0.5672277
##         Cellsteam     Roast   Sonroast  Cellroast
## sample1 0.2956562 0.8187462  0.6450521  0.5485523
## sample2 0.2956562 0.8187462  0.6450521  0.5485523
## sample3 0.2956562 0.8187462  0.6450521  0.5485523
## sample4 0.2956562 0.4555785  0.6450521  0.5485523
## sample5 1.5183751 0.7784026  0.6450521 -1.3296666
## sample6 0.2956562 0.6138959 -1.0411406  0.5485523

使用pheatmap函数可视化热力图

##            lab
## Raw       lab1
## Soak      lab1
## Sonicate  lab1
## Cellulase lab1
## Steam     lab1
## Sonsteam  lab2
##         group
## sample1    G1
## sample2    G1
## sample3    G1
## sample4    G1
## sample5    G1
## sample6    G1

## 5.4 可视化雷达图

## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:igraph':
## 
##     as_data_frame, groups, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## Loading required package: maps
## Loading required package: mapdata
## Parsed with column specification:
## cols(
##   province = col_character(),
##   city = col_character(),
##   town = col_character(),
##   temperature = col_double(),
##   relative_humidity = col_double(),
##   rainfall = col_double(),
##   wind_direction = col_double(),
##   wind_strong = col_double(),
##   day = col_date(format = ""),
##   hour = col_character(),
##   year = col_double(),
##   month = col_double(),
##   days = col_character()
## )
## # A tibble: 6 x 13
##   province city  town  temperature relative_humidi… rainfall wind_direction
##   <chr>    <chr> <chr>       <dbl>            <dbl>    <dbl>          <dbl>
## 1 上海     上海  嘉定           11               96        0             92
## 2 上海     上海  嘉定           11               96        0             81
## 3 上海     上海  嘉定            8               94        0            103
## 4 上海     上海  嘉定            8               95        0             79
## 5 上海     上海  嘉定           10               97        0             76
## 6 上海     上海  嘉定            8               95        0             92
## # … with 6 more variables: wind_strong <dbl>, day <date>, hour <chr>,
## #   year <dbl>, month <dbl>, days <chr>
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0    77.0   256.0   200.5   302.0   359.0
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  -12.00   66.75  237.00  186.17  295.00  347.00
##    N  NNE   NE  ENE    E  ESE   SE  SSE    S  SSW   SW  WSW    W  WNW   NW  NNW 
##  592  587  541  606  553  288  187  129  130  135  161  394  842 1157  933  685
##              N NNE  NE ENE   E ESE  SE SSE   S SSW  SW WSW   W WNW  NW NNW
## [0,3.25]   476 478 469 552 483 244 165 124 123 132 147 381 677 794 621 510
## (3.25,6.5] 102  89  62  49  69  41  22   5   7   3  13  12 146 312 271 151
## (6.5,9.75]  12  17  10   5   1   3   0   0   0   0   1   1  15  43  38  22
## (9.75,13]    2   3   0   0   0   0   0   0   0   0   0   0   4   8   3   2

##    A  B  C  D  E DE
## 1 10 10 10 10  5 10
## 2  0  0  0  0  0  0
## 3  8  6 10  5  3  7
## 4  4  8  7  7 NA 10
## 5  6  9  3 10  4  8

## , , Sex = Male
## 
##        Eye
## Hair    Brown Blue Hazel Green
##   Black    32   11    10     3
##   Brown    53   50    25    15
##   Red      10   10     7     7
##   Blond     3   30     5     8
## 
## , , Sex = Female
## 
##        Eye
## Hair    Brown Blue Hazel Green
##   Black    36    9     5     2
##   Brown    66   34    29    14
##   Red      16    7     7     7
##   Blond     4   64     5     8
##       Brown.Male Blue.Male Hazel.Male Green.Male Brown.Female Blue.Female
## Black         32        11         10          3           36           9
## Brown         53        50         25         15           66          34
## Red           10        10          7          7           16           7
## Blond          3        30          5          8            4          64
##       Hazel.Female Green.Female
## Black            5            2
## Brown           29           14
## Red              7            7
## Blond            5            8
## [1] "Black" "Brown" "Red"   "Blond"

5.5 可视化韦恩图,集合数据的可视化方式。

## Loading required package: grid
## Loading required package: futile.logger

##   element one two three four five six
## 1       1   1   1     0    0    0   0
## 2       2   1   0     0    1    0   0
## 3       3   1   1     0    0    0   1
## 4       4   1   0     0    0    0   0
## 5       5   1   1     0    0    0   0
## 6       6   1   0     0    1    0   1

5.6 可视化树图

树图和可交互树图

##   iso3          country     continent population    GNI
## 3  BMU          Bermuda North America      67837 106140
## 4  NOR           Norway        Europe    4676305 103630
## 5  QAT            Qatar          Asia     833285  92200
## 6  CHE      Switzerland        Europe    7604467  88120
## 7  MAC Macao SAR, China          Asia     559846  76270
## 8  LUX       Luxembourg        Europe     491775  75990

5.7 可视化马赛克图

2个变量的马赛克图、多个变量的马赛克图

##        Eye
## Hair    Brown Blue Hazel Green
##   Black    32   11    10     3
##   Brown    53   50    25    15
##   Red      10   10     7     7
##   Blond     3   30     5     8

##        Eye
## Hair    Brown Blue Hazel Green
##   Black    32   11    10     3
##   Brown    53   50    25    15
##   Red      10   10     7     7
##   Blond     3   30     5     8
## 
## Attaching package: 'vcd'
## The following object is masked from 'package:fmsb':
## 
##     oddsratio

5.7 文本数据可视化

可视化词云

## Loading required package: RColorBrewer
##         V2   V1
## 1     数据 2304
## 3     统计 1413
## 4     用户  855
## 5     模型  846
## 7     分析  773
## 8 数据分析  750

##          groupA groupB
## 数据       2304     38
## 统计       1413     38
## 用户        855     38
## 模型        846     38
## 分析        773     38
## 数据分析    750     38
## Warning in brewer.pal(2, "Dark2"): minimal value for n is 3, returning requested palette with 3 different levels

##          word freq
## oil       oil   85
## said     said   73
## prices prices   48
## opec     opec   42
## mln       mln   31
## the       the   26