作者:带土;Daitu; Adam 邮箱:2505131775@qq.com
社交网络图,邻接矩阵可视化
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
library(igraphdata)
## 1:通过数据表格获得网络图,并可视化不同网络布局的网络图
## 读取数据
edge <- read.csv("data/edge.csv")
vertex <- read.csv("data/vertex.csv")
## Country:国家,airportnumber:机场数量,vtype:节点的类型
head(vertex)## 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 . . . . . . . . . . . . . . . . .
## 可视化时,对网络数据的显示图像进行调整
# 生成节点的颜色和形状
V(karate)$color <- colrs[V(karate)$color]
## 单独设置主管和教练节点的颜色
V(karate)[V(karate)$label %in% c("H","A")]$color <- "green"
## 调整节点的形状
V(karate)$shape <- "circle"
V(karate)[V(karate)$label %in% c("H","A")]$shape <- "sphere"
## 设置节点的大小
V(karate)$size <- 15+degree(karate)
## 设置边的粗细
E(karate)$width <- E(karate)$weight
## 可视化调整后的网络图
set.seed(1234)
plot(karate,layout = layout.fruchterman.reingold)## 有向的网络图
g3 <- graph_from_literal( Alice +-+ Bob --+ Cecil +-- Daniel,
Eugene --+ Gordon:Helen )
plot(g3)## 通过邻接矩阵可视化网络图
set.seed(123)
## 使用随机数生成一个邻接矩阵
adjm <- matrix(sample(0:1, 100, replace=TRUE,
prob=c(0.8,0.2)), nc=10)
colnames(adjm) <- letters[1:10]
rownames(adjm) <- letters[1:10]
set.seed(123)
# plot 4个图 - 2 rows, 2 columns,每个图使用网络声称模式
par(mfrow=c(2,2), mar=c(0,0,1,0),family = "STKaiti")
plot(graph_from_adjacency_matrix(adjm,
## 有向网络
mode = "directed"),
main = 'mode = "directed"',
layout = layout_with_fr)
plot(graph_from_adjacency_matrix(adjm,
## 无向网络"undirected"或"max"
mode = "undirected"),
main = 'mode = "undirected"',
layout = layout_with_fr)
plot(graph_from_adjacency_matrix(adjm,
## 无向网络,只使用上三角的数据
mode = "upper"),
main = 'mode = "upper"',
layout = layout_with_fr)
plot(graph_from_adjacency_matrix(adjm,
## 无向网络,只使用下三角的数据
mode = "lower"),
main = 'mode = "lower"',
layout = layout_with_fr)## 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 .
无坐标数据的地图数据可视化
## 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
## 可视化世界地图,根据人口分布来配色
## 1:将国家数据和地图根据ISO3联系再一起
Map1 <- joinCountryData2Map(GNI2014,
## 根据iso3数据中的编码来匹配区域
joinCode = "ISO3",
nameJoinColumn = "iso3")## 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
## 2:将国家数据和地图根据名称联系再一起
Map2 <- joinCountryData2Map(GNI2014,
## 根据名称数据中的编码来匹配区域
joinCode = "NAME",
nameJoinColumn = "country")## 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
## 使用Map1数据可视化地图
p1 <- mapCountryData(mapToPlot = Map1,
## 想要可视化的数据,人口
nameColumnToPlot = "population",
addLegend=FALSE,## 先不显示图例
## 根据数据的多少设置颜色
catMethod = "pretty",colourPalette="rainbow",
## 定义海洋的颜色,和没匹配数据国家的颜色
oceanCol = "steelblue1",missingCountryCol = "white",
mapTitle = "Population for Country")
##为图像添加图例,并对齐进行调整
do.call(addMapLegend,c(p1,legendLabels="all",
## 颜色条的宽度和
legendWidth=0.5,
## 根据数据划分图例的颜色条
legendIntervals="data",
## 图例离图像下边界的距离
legendMar = 4))## 可视化局部地区的图像,使用曲对数后的GNI数据
p2 <- mapCountryData(mapToPlot = Map2, ## 使用根据国家匹配的数据
## 想要可视化的数据,人口
nameColumnToPlot = "log2GNI",
addLegend=FALSE,## 先不显示图例
## 根据数据的多少设置颜色
catMethod = "fixedWidth",colourPalette="heat",
## 定义海洋的颜色,和没匹配数据国家的颜色
oceanCol = "steelblue1",missingCountryCol = "white",
mapRegion="asia",
mapTitle = "GNI for Country")
##为图像添加图例,并对齐进行调整
do.call(addMapLegend,c(p2,legendLabels="all",
## 颜色条的宽度和
legendWidth=0.5,
## 根据数据划分图例的颜色条
legendIntervals="data",
## 图例离图像下边界的距离
legendMar = 4,
## 垂直的图例
horizontal=FALSE))## 使用点的大小来可视化国家的人口多少
par(mar = c(0,0,0,0))
p3 <- mapBubbles(Map1,
# 点的大小
nameZSize = "population",
## 点的颜色
nameZColour = "continent",
colourPalette = "rainbow",
oceanCol = "steelblue1",
missingCountryCol = "white",
landCol="wheat",pch=21,
mapRegion = "world")详细介绍pheatmap包对数据进行可视化和可视化相关系数的包
## corrplot 0.84 loaded
library(readxl)
## 读取数据
heatdata <- read.csv("data/heatmapdata2.csv")
rownames(heatdata) <- heatdata$data
heatdata$data <- NULL
head(heatdata)## 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
## 可视化相关系数热力图
heat_cor <- cor(heatdata)
## 使用corrplot包可视化相关系数热力图
par(family = "STKaiti")
corrplot(heat_cor,
## "circle", "square", "ellipse", "number", "pie"等
method = "ellipse",
## "full", "lower", "upper"
type = "full",
title = "corrplot",
diag = TRUE,
mar = c(0, 0, 2, 0),
## 调整文本标签,变量的名称
tl.cex = 0.8,tl.col = "black",
tl.offset = 0.5, tl.srt = 60,
## lt:left,top;ld:left,diagonal;td;d;n:none;
tl.pos = "lt",
## 调整颜色条,r:right;b:bttom;n:none;
cl.pos = "r",cl.cex = 0.8)## 使用corrplot.mixed函数可视化相关系数热力图
par(family = "STKaiti")
corrplot.mixed(heat_cor,
## 下三角使用数字,上三角使用圆
lower = "number", upper = "circle",
## 调整文本标签,变量的名称
tl.col="black",tl.pos = "d",
tl.cex = 0.8,tl.srt = 60,
## 调整数字的显示大小
number.cex = 0.8,
mar = c(0, 0, 2, 0),
main = "相关系数热力图",
lower.col = "black")## 调整横纵坐标标签字体的大小
pheatmap(t(heatdata),cluster_rows = FALSE,cluster_cols = FALSE,
main = "heatmap",
fontsize_row = 10, fontsize_col = 8)## 显示出数字
pheatmap(heatdata,cluster_rows = FALSE,cluster_cols = FALSE,
main = "heatmap",
fontsize_row = 7, fontsize_col = 10,
## 对数字进行调整
display_numbers = TRUE, number_format = "%.2f",
fontsize_number = 5)## 固定每个单元的大小
pheatmap(heatdata,cluster_rows = FALSE,cluster_cols = FALSE,
main = "heatmap",
fontsize_row = 7, fontsize_col = 10,
## 对数字进行调整
display_numbers = TRUE, number_format = "%.2f",
fontsize_number = 6,
cellwidth = 20, cellheight = 8)## 在聚类热力图中对聚类结果进行划分
pheatmap(t(heatdata),cluster_rows = FALSE,
main = "heatmap",
fontsize_row = 10, fontsize_col = 8,
cluster_cols = TRUE,cutree_cols = 4,
gaps_col = TRUE)## 为热力图的行或列生成注释,通常适合数据有分组数据的情况
ann_row <- data.frame(lab = as.factor(rep(c("lab1","lab2"),c(5,5))))
rownames(ann_row) <- colnames(heatdata)
head(ann_row)## lab
## Raw lab1
## Soak lab1
## Sonicate lab1
## Cellulase lab1
## Steam lab1
## Sonsteam lab2
## 设置列标签的数据分组情况
ann_col <- data.frame(group = as.factor(rep(c("G1","G2","G3"),
c(20,15,8))))
rownames(ann_col) <- rownames(heatdata)
head(ann_col)## group
## sample1 G1
## sample2 G1
## sample3 G1
## sample4 G1
## sample5 G1
## sample6 G1
pheatmap(t(heatdata),cluster_rows = FALSE,cluster_cols = FALSE,
main = "heatmap",
fontsize_row = 10, fontsize_col = 8,
annotation_row = ann_row,annotation_col = ann_col)## 调整热力图的颜色
pheatmap(t(heatdata),cluster_rows = FALSE,cluster_cols = FALSE,
## 颜色插值函数
color = colorRampPalette(
c("navy", "white","firebrick3"))(50),
main = "heatmap",
fontsize_row = 10, fontsize_col = 8,
annotation_row = ann_row,annotation_col = ann_col)pheatmap(t(heatdata),cluster_rows = FALSE,cluster_cols = TRUE,
## 颜色插值函数
color = colorRampPalette(
c("red", "white", "blue"))(100),
main = "heatmap",
fontsize_row = 10, fontsize_col = 8,
annotation_row = ann_row,annotation_col = ann_col) ## 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
## 将风向划分为16个区间,代表16个方向
## 切分方向
dirbreak <- seq(-12.25,360,22.5)
## 代表的方向
direction <-c("N","NNE","NE","ENE","E","ESE","SE","SSE",
"S","SSW","SW","WSW","W","WNW","NW","NNW")
## 调整角度
index <- weather$wind_direction > 347.25
weather$wind_direction[index] <- weather$wind_direction[index] - 360
summary(weather$wind_direction)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -12.00 66.75 237.00 186.17 295.00 347.00
weather$wind_direction2 <- cut(weather$wind_direction,
breaks = dirbreak,
labels = direction,
include.lowest = TRUE)
summary(weather$wind_direction2)## 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
## 将风力的强弱划分为4各等级
weather$wind_strong2 <- cut_interval(weather$wind_strong,4)
## 计算不同风向和等级对应下出现的次数
shwind <- as.data.frame.array(table(weather$wind_strong2,
weather$wind_direction2))
shwind## 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
## 可视化风向雷达图
par(family = "STKaiti")
rosavent(shwind,
## 参考圆的数量
fnum=4,
## 两个个参考员之间的百分比
fint=4, ang=-3*pi/16,
margen=c(0,0,2,0),
## 填充使用的颜色
col=rainbow(4,0.5,0.92,start=0.1,end=0.9),
main="一个月实时风向雷达图")## 针对其它数据可视化雷达图,使用fmsb包
library(fmsb)
## 数据准备
## 数据表的前两行,必须时每个变量取值的最大值和最小值
maxmin <- data.frame(A = c(10,0),B = c(10,0),C = c(10,0),
D = c(10,0),E = c(5,0),DE = c(10,0))
one <- data.frame(A = 8,B = 6,C = 10,D = 5,E = 3, DE = 7)
two <- data.frame(A = 4,B = 8,C = 7,D = 7,E = NA, DE = 10)
three <- data.frame(A = 6,B = 9,C = 3,D = 10,E = 4, DE = 8)
## 将数据连接到一起
plotdata <- rbind(maxmin,one,two,three)
plotdata## 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
radarchart(plotdata,
axistype = 0, ## 坐标轴的样式可以0:5
seg = 5, ## 每个坐标轴网格的切分情况
## 颜色可以直接指定,也可以使用数字
pcol = c("red","blue","green"),
plty = 1,
## 线的粗细
plwd = 1,
## 设置多边形的填充
pdensity = c(30,30,30),
pangle = c(45,90,135),
pfcol = c("red","blue","green"),
## 设置雷达图的网格
cglty = 3,
cglcol = "lightblue")radarchart(plotdata,
axistype = 2, ## 坐标轴的样式可以0:5
seg = 5, ## 每个坐标轴网格的切分情况
pcol = 1:3,
## 线的类型,支持1:8,重复循环使用
plty = 3:5,
plwd = 2,
## 设置坐标轴和数值的颜色
axislabcol = "black",
title = "plot data",
## 缺失值是否进行插值,默认为FALSE
na.itp = TRUE,
## 中心是否从0开始
centerzero = TRUE,
## 调整变量的名称,默认为数据表的变量名
vlabels = c("apple","banana","watermelon",
"pear","orange\njuice"),
vlcex = 0.7)## , , 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"
radarchart(HairEyeColor,
axistype = 2, ## 坐标轴的样式可以0:5
seg = 4, ## 每个坐标轴网格的切分情况
## 颜色可以直接指定,也可以使用数字
pcol = c("black","brown","red","yellow"),
## 线的粗细
plwd = 2,plty = 1,
maxmin = FALSE)## Loading required package: grid
## Loading required package: futile.logger
library(UpSetR)
## 数据准备
one <- 1:100
two <- seq(1,200,by = 2)
three <- seq(10,300,by = 5)
four <- seq(2,400,by = 4)
five <- seq(10,500,by = 10)
vcol <- c("red","blue","green","DeepPink","Gold")
## 将数据列表绘制为韦恩图,图片格式默认为tiff
fig1 <- venn.diagram(list(A = one,B = two,C = three,
D = four,E = five),
filename = NULL,
## 设置标题
main.fontfamily = "STKaiti",
main = "韦恩图",sub = "sub title",
sub.pos = c(0.1,1),
lwd = 1,##线宽
fill = vcol,alpha = 0.5,## 填充颜色
margin = 0.1)
grid.newpage()
grid.draw(fig1)grid.newpage()
## 可视化两个集合的韦恩图,
fig2 <- draw.pairwise.venn(area1 = 10,area2 = 10,cross.area = 5,
category = c("one","two"),
col = c("red","blue"),
fill = c("red","blue"),alpha = 0.6)
grid.newpage()
grid.draw(fig2)## 韦恩图最多可以可视化5个集合的交集和并集的情况,而upset包可以分析更多个集合之间的关系
## 使用upset包可视化集合之间的交集、并集的情况
## 数据准备
six <- seq(3,400,by = 3)
## 1: 将5个集合的并集就算出来,
all <- unique(c(one,two,three,four,five,six))
## 建立一个数据表格
plotdata <- data.frame(matrix(nrow = length(all),ncol = 7))
colnames(plotdata) <-c("element","one","two","three","four",
"five","six")
## 2:数据表第一列是5个集合的并集的所有元素
plotdata[,1] <- all
## 3:其它列中的对应行,如果包含那一行的元素,则取值为1,否则取值为0
for (i in 1:length(all)) {
plotdata[i,2] <- ifelse(all[i] %in% one,1,0)
plotdata[i,3] <- ifelse(all[i] %in% two,1,0)
plotdata[i,4] <- ifelse(all[i] %in% three,1,0)
plotdata[i,5] <- ifelse(all[i] %in% four,1,0)
plotdata[i,6] <- ifelse(all[i] %in% five,1,0)
plotdata[i,7] <- ifelse(all[i] %in% six,1,0)
}
## 查看数据
head(plotdata)## 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
## 可视化6个集合的交并情况
upset(plotdata,
sets = c("one","two","three","four","five","six"),
nintersects = 40, ## 默认显示前40个交集
order.by = "freq", ## 根据频数排序
## 设置主条形图
matrix.color = "blue", ## 数据矩阵的颜色
main.bar.color = "red",# 主要条形图的颜色
mainbar.y.label = "Intersection Size",
##设置集合条形图
sets.bar.color = "green",
sets.x.label = "Set size",
## 设置矩阵点图
point.size = 3,line.size = 0.5,
## 矩阵点图和条形图的比例
mb.ratio = c(0.65, 0.35))## 条形图生序排放
upset(plotdata,
sets = c("one","two","three","four","five","six"),
nintersects = 40, ## 默认显示前40个交集
order.by = "freq", ## 根据频数排序
## 矩阵点图和条形图的比例
mb.ratio = c(0.65, 0.35),
## 条形图生序排放,并且不显示数字
decreasing = FALSE,show.numbers = FALSE,
## 阴影的颜色
shade.color= "lightblue",shade.alpha = 0.5,
matrix.dot.alpha = 0.5,
## 对交集的变换,"identity", "log10", "log2"
scale.intersections = "identity",
## 对集和数量的变换,"identity", "log10", "log2"
scale.sets = "log2",
## 对文本进行缩放
text.scale = 1.3)树图和可交互树图
## 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
## 通过人口来的多少进行颜色填充
treemap(GNI2014,index = "country",vSize = "GNI",
## 设置填充颜色,默认根据index的取值进行填充
vColor = "population",type = "value",
title = "World GNI and population")## 改变填充颜色的调色盘
treemap(GNI2014,index = "country",vSize = "GNI",
## 设置填充颜色,默认根据index的取值进行填充
vColor = "population",type = "value",
palette = "RdYlBu",
title = "World GNI and population")## 对数据使用两个分组变量进行索引
treemap(GNI2014,index = c("continent","country"),
vSize = "population", ## 大小
## 设置填充颜色,默认根据index的取值进行填充
vColor = "GNI",type = "value",
palette = "RdYlBu",
title = "World GNI and population",
n = 10, ## 连续数值切分的份数
## 图例位置"bottom", "right", or "none"
position.legend = "right")## 使用d3treeR包绘制可交互的树图
## 1:先使用treemap函数绘制树图
tmap <- treemap(GNI2014,index = c("continent","country"),
vSize = "population", ## 大小
vColor = "GNI",type = "value")
## 模式1
d3tree(tmap,rootname = "World GNI and population")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
data("Titanic")
## 2个变量的马赛克图
## 1 : 使用mosaicplot {graphics}函数
## 可视化二维列联表
mosaicplot(HairEyeColor[,,1],main = "mosaicplot",
## 使用灰度渐变色
color = TRUE)## 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
可视化词云
## Loading required package: RColorBrewer
## V2 V1
## 1 数据 2304
## 3 统计 1413
## 4 用户 855
## 5 模型 846
## 7 分析 773
## 8 数据分析 750
## 使用wordcloud包可视化数据
## 1: textplot()函数在指定的位置可视化文本
set.seed(1234)
x <- 1:10
y <- 1:10
par(family = "STKaiti")
wordcloud::textplot(x,y,demoFreqC$V2[1:10])## 2:wordcloud()函数可视化词云
par(family = "STKaiti")
wordcloud(words = demoFreqC$V2,freq = demoFreqC$V1,
scale=c(4,.5), ## 可视化时词的大小范围
min.freq=50, ## 词频小于min.freq的不可视化
max.words=200 ## 最多可视化200个词
)## 设置可视化词云时的颜色
pal <- brewer.pal(10,"BrBG")
par(family = "STKaiti")
wordcloud(words = demoFreqC$V2,freq = demoFreqC$V1,
scale=c(4,.5), ## 可视化时词的大小范围
min.freq=50, ## 词频小于min.freq的不可视化
max.words=200, ## 最多可视化200个词
colors = pal)## 可视化分组数据的对比词云
## 数据准备,生成数据框
groupdata <- data.frame(groupA = demoFreqC$V1,
groupB = rev(demoFreqC$V1))
rownames(groupdata) <- demoFreqC$V2
head(groupdata)## groupA groupB
## 数据 2304 38
## 统计 1413 38
## 用户 855 38
## 模型 846 38
## 分析 773 38
## 数据分析 750 38
par(family = "STKaiti")
comparison.cloud(groupdata,scale=c(4,.5),max.words=100,
## 设置颜色
colors=brewer.pal(2,"Dark2"),
## 设置标题
title.size=2,title.colors="blue")## Warning in brewer.pal(2, "Dark2"): minimal value for n is 3, returning requested palette with 3 different levels
## 增加数据的组数,可视化多组数据的对比图像
set.seed(123)
groupdata$groupC <- demoFreqC$V1[sample(nrow(groupdata))]
set.seed(1)
groupdata$groupD <- demoFreqC$V1[sample(nrow(groupdata))]
par(family = "STKaiti")
comparison.cloud(groupdata,scale=c(4,.5),max.words=50,
## 设置颜色
colors=brewer.pal(4,"Set1"),
## 设置标题
title.size=1.5,title.colors="black")## word freq
## oil oil 85
## said said 73
## prices prices 48
## opec opec 42
## mln mln 31
## the the 26
## 3:wordcloud2函数根据字母的形状可视化词云
## 圆形词云
wordcloud2(demoFreq,size = 1, ## 字体大小
## 设置字体
fontFamily = "Segoe UI", fontWeight = "bold",
## 设置颜色,'random-dark' 或者 'random-light'
color = "random-dark", backgroundColor = "white",
## 设置形状, 默认圆形
shape = "circle")## 心形
wordcloud2(demoFreqC,size = 1, ## 字体大小
## 设置字体
fontFamily = "STKaiti",
## 设置颜色,'random-dark' 或者 'random-light'
color = "random-light", backgroundColor = "grey",
## 设置形状, 心形
shape = "cardioid")## 菱形,设置文字的倾斜角度
wordcloud2(demoFreqC[1:200,],size = 1, ## 字体大小
## 设置字体
fontFamily = "STKaiti",
## 设置颜色,'random-dark' 或者 'random-light'
color = "random-light", backgroundColor = "white",
## 设置形状, 心形
shape = "diamond",
## 文字的倾斜角度
minRotation = -pi/6, maxRotation = -pi/6,
rotateRatio = 1)## 五角星形
wordcloud2(demoFreqC[1:100,],size = 1, ## 字体大小
## 设置字体
fontFamily = "STKaiti",
## 设置颜色,'random-dark' 或者 'random-light'
color = "random-light", backgroundColor = "white",
## 设置形状, 五角星形
shape = "star")