R-可视化--饼图与环图
1、pie() pie()是基础的绘制饼图函数,用法简单 1 2 3 4 5 6 7 8 pie(Prop, labels = LETTERS[1:5], main = "Test Piechart") # labels:设置标签 # radius:设置半径,default 0.8 # clockwise:设置是否顺时针,默认为F # init.angle:设置起始单元的角度 # col:设置颜色 # border与lty分别设置边的颜色与形状 ...
1、pie() pie()是基础的绘制饼图函数,用法简单 1 2 3 4 5 6 7 8 pie(Prop, labels = LETTERS[1:5], main = "Test Piechart") # labels:设置标签 # radius:设置半径,default 0.8 # clockwise:设置是否顺时针,默认为F # init.angle:设置起始单元的角度 # col:设置颜色 # border与lty分别设置边的颜色与形状 ...
1、ggplot geom_text()通过交代文本的坐标位置与内容绘制,通常与点图联用。相关参数包括: color 标签的颜色 alpha 标签的透明度 check_overlap 若有重叠的标签则不显示,默认为False 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 library(tidyverse) library(patchwork) p = ggplot(mpg, aes(displ, hwy)) + geom_point() # 设置候选标签 best_in_class <- mpg %>% group_by(class) %>% filter(row_number(desc(hwy)) == 1) # nudge_x / nudge_y:设置标签的相对位置,移动单位与x/y轴匹配;大于0分别表示右、上 # hjust / vjust:设置标签的相对位置,移动单位与自身长宽匹配;小于0分别表示右,上 p1 = p + geom_text(aes(label = model), data = best_in_class) p2 = p + geom_text(aes(label = model), data = best_in_class, nudge_x = 1, nudge_y = 10) p1 | p2 geom_label()在上述的基础上设置文本框,相关参数包括: label.size:框轮廓线的宽度 label.r:框轮廓的平滑度 label.padding:框的大小 alpha:框的透明度 1 2 3 4 p1 = p + geom_label(aes(label = model), data = best_in_class) p2 = p + geom_label(aes(label = model), data = best_in_class, alpha=0.5, fill="grey", color="red") p1 | p2 2、ggrepel 上述函数对于重叠标签的处理能力有限,ggrepel包可对此进行有效的补充。简单理解就是确定文本标签的坐标后,对于存在互相重叠的标签进行适当偏移,使各自均能完整的显示。 ...
1 2 3 4 5 6 # install.packages("corrplot") library(corrplot) # 计算相关性矩阵 M<-cor(mtcars) dim(M) # [1] 11 11 1、可视化R系数方式 method参数提供了如下6种可视化方式 circle(default), square, ellipse, number, shade, color, pie 1 corrplot(M, method="ellipse") 2、对称布局方式 type参数提供了如下3种布局方式 full(default), upper, lower 1 corrplot(M, type="lower") 3、样本排序 order参数提供了如下 original(default), AOE, FPC, hclust, alphabet 如果采用hclust,可以通过hclust.method参数设置聚类方法 1 corrplot(M, order="hclust") 4、颜色方案 (1)相关性颜色 1 2 3 col = colorRampPalette(c("blue", "white", "red"))(20) # 连续型 col = RColorBrewer::brewer.pal(n=8, name="RdBu") #离散型 corrplot(M, col=col) (2)其它颜色 1 2 3 4 5 # 背景填充颜色 corrplot(M, bg="grey") # 方格边框颜色 corrplot(M, addgrid.col = "black") 5、文本属性设置 (1)坐标轴名:通过tl.系列参数设置大小,颜色、角度等 1 corrplot(M, tl.col="black", tl.srt=45) (2)标注相关性系数 1 corrplot(M, addCoef.col = "black", number.digits = 1) (3)标注P值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ## 首先得到P值矩阵 cor.mtest <- function(mat, ...) { mat <- as.matrix(mat) n <- ncol(mat) p.mat<- matrix(NA, n, n) diag(p.mat) <- 0 for (i in 1:(n - 1)) { for (j in (i + 1):n) { tmp <- cor.test(mat[, i], mat[, j], ...) p.mat[i, j] <- p.mat[j, i] <- tmp$p.value } } colnames(p.mat) <- rownames(p.mat) <- colnames(mat) p.mat } p.mat <- cor.mtest(mtcars) dim(p.mat) # [1] 11 11 sig.level 参数设置显著阈值 insig参数设置标注方式,可选项包括 pch(default):将不显著的,标注为叉 blank:将不显著的,不显示 n:不作处理 label_sig 将显著的,标注星号 1 corrplot(M, p.mat = p.mat, sig.level = 0.01, insig = "blank") 1 2 3 corrplot(M, p.mat = p.mat, sig.level = c(0.1, 0.01), insig = "label_sig", pch.cex = 1.5, pch.col = "red") 6、其它参数 is.corr参数, 默认为TRUE;即输入矩阵是否为相关性矩阵 1 corrplot(scale(t(mtcars)), is.corr = FALSE) diag参数,默认为TRUE;即是否显示对角线的相关性 1 corrplot(M, diag=FALSE)
示例数据 1 2 3 4 5 6 7 8 9 10 11 12 data("mtcars") df <- mtcars df$name <- rownames(df) df$cyl <- as.factor(df$cyl) head(df[,c("name","cyl","mpg","wt")]) # name cyl mpg wt # Mazda RX4 Mazda RX4 6 21.0 2.620 # Mazda RX4 Wag Mazda RX4 Wag 6 21.0 2.875 # Datsun 710 Datsun 710 4 22.8 2.320 # Hornet 4 Drive Hornet 4 Drive 6 21.4 3.215 # Hornet Sportabout Hornet Sportabout 8 18.7 3.440 # Valiant Valiant 6 18.1 3.460 方式1: cowplot 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 library(cowplot) p_main = ggplot(df, aes(x = wt, y = mpg, color = cyl))+ geom_point() p_right = axis_canvas(p_main, axis = "x")+ geom_density(data = df, aes(x = wt, fill = cyl), alpha = 0.7, size = 0.2) p_top = axis_canvas(p_main, axis = "y", coord_flip = TRUE)+ geom_density(data = df, aes(x = mpg, fill = cyl), alpha = 0.7, size = 0.2)+ coord_flip() p1 = insert_xaxis_grob(p_main, p_right, grid::unit(.2, "null"), position = "top") p2 = insert_yaxis_grob(p1, p_top, grid::unit(.2, "null"), position = "right") ggdraw(p2) 方式2: patchwork 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 library(patchwork) p_main = ggplot(df, aes(x = wt, y = mpg, color = cyl)) + geom_point() + theme(legend.position="bottom") p_top = ggplot(df, aes(x = wt, color = cyl)) + geom_boxplot() + theme_void() + theme(legend.position = "none") p_right = ggplot(df, aes(x = mpg, color = cyl)) + geom_boxplot() + theme_void() + theme(legend.position = "none") + coord_flip() p = p_top + plot_spacer() + p_main + p_right + plot_layout(ncol = 2, nrow = 2, heights = c(1, 5), widths = c(5, 1)) ...
1 2 3 library(ggplot2) # install.packages("ggbeeswarm") library(ggbeeswarm) 0、示例数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 set.seed(1995) y <- round(rnorm(200), 1) df <- data.frame(y = y, group = sample(c("G1", "G2", "G3"), size = 200, replace = TRUE)) head(df) # y group # 1 1.1 G3 # 2 -0.3 G2 # 3 0.1 G2 # 4 0.4 G1 # 5 1.7 G3 # 6 -0.3 G3 1、基础绘图 1 2 ggplot(df, aes(x = group, y = y)) + geom_beeswarm() 2、cex参数设置点的间距 1 2 ggplot(df, aes(x = group, y = y)) + geom_beeswarm(cex = 3) 3、priority参数设置点的布局方式, ...
Github tutorial:https://github.com/const-ae/ggsignif 1 2 library(ggplot2) library(ggsignif) 1、comparisons设置分组,计算并标注P值 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ggplot(mpg, aes(class, hwy)) + geom_boxplot() + geom_signif( comparisons = list(c("compact", "midsize"), c("minivan", "suv")), map_signif_level = TRUE, textsize = 6, test = "t.test", # c("wilcox.test","t.test") vjust = -0.5 # 标签相对位置 ) + ylim(NA, 48) # map_signif_level: 将P值映射为等级 # test: 设置差异分析方法 # size、textsize分别设置线与标签的颜色 annotation直接交代P值/标签,手动设置 1 2 3 4 5 6 7 8 9 10 11 12 13 anno <- t.test( iris$Sepal.Width[iris$Species=="versicolor"], iris$Sepal.Width[iris$Species == "virginica"] )$p.value # [1] 0.001819483 ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) + geom_boxplot(position = "dodge") + geom_signif( annotation = c(formatC(anno, digits = 1),"***"), y_position = c(4, 4.5), xmin = c(2,1), xmax = c(3,2), # 位置 tip_length = 0.02, ) 混合叠加多次注释 1 2 3 4 5 6 7 8 9 10 11 ggplot(dat, aes(Group, Value)) + geom_bar(aes(fill = Sub), stat = "identity", position = "dodge", width = .5) + geom_signif( y_position = c(5.3, 8.3), xmin = c(0.8, 1.8), xmax = c(1.2, 2.2), annotation = c("**", "NS"), tip_length = 0 ) + geom_signif( comparisons = list(c("S1", "S2")), y_position = 9.3, tip_length = 0, vjust = 0.2 ) + scale_fill_manual(values = c("grey80", "grey20"))
https://github.com/funkyheatmap/funkyheatmap 1 2 3 4 5 6 7 8 # install.packages("funkyheatmap") library(funkyheatmap) library(tidyverse) data("mtcars") vignette("dynbenchmark", "funkyheatmap") #vignette("mtcars", "funkyheatmap") 1、简单用法 对于文本列,直接显示相应文本;对于数值列,使用几何图形映射数值大小。 ...
tidyr是tidyverse系列的组成工具包之一,其主要功能侧重于表格的reshaping and organizing. 下面简要学习其几个比较常用的函数。 https://tidyr.tidyverse.org/ https://github.com/rstudio/cheatsheets/blob/main/tidyr.pdf 1 library(tidyverse) 1. 表格长宽转换 类似reshape2包 ...