欢迎光临散文网 会员登陆 & 注册

绘制脊型图和马赛克图/SCI论文/科研/研究生/生信分析热点思路

2023-01-03 09:28 作者:尔云间  | 我要投稿


    要绘制脊型图需要用到spineplot函数。首先我们需要建立两个向量,作为绘图的材料。

treatment <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2),

                    labels = c("placebo", "treated"))

improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)),

                   levels = c(1, 2, 3),

                   labels = c("none", "some", "marked"))

    注意需要因子型向量哦。

只需要一行spineplot(improved ~ treatment),基本的图形就出来了。

    为了节省是我们直接使用R自带的数据集UCBAdmissions,这里面有适合画脊型图的数据。

spineplot(marginSums(UCBAdmissions, c(3, 2)),main = "Applications at UCB")

spineplot(marginSums(UCBAdmissions, c(3, 1)),main = "Admissions at UCB")

    在spineplot函数中,会默认以行作为横坐标,以列为纵坐标对数据进行分型,非常方便。我们平时作图用到的数据多为表格,这种用法更常见,但以向量作为输入会更灵活。比如下方操作。

fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1,

                 1, 1, 1, 2, 1, 1, 1, 1, 1),

               levels = c(1, 2), labels = c("no", "yes"))

temperature <- c(53, 57, 58, 63, 66, 67, 67, 67, 68, 69, 70, 70,

                 70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81)

spineplot(fail ~ temperature)

spineplot(fail ~ temperature, breaks = 3)

spineplot(fail ~ temperature, breaks = quantile(temperature))

    通过调试break参数可以完成对图形更精细的控制,调整变量分布范围。

展示数据有一种更加直观的方法——马赛克图,绘制马赛克图需要用到函数mosaicplot。

mosaicplot(marginSums(UCBAdmissions, c(3, 2)),main = 'masaike',color = c('green','red'),ylab = 'sex')

    马塞图与脊型图有相似之处,若是变量为布尔型,脊型图会更加合适,若是变量数目较多就只能画马赛克图。在面对不同函数时,画图用到的很多参数都是相似的,学习时可以互相借鉴能够更快掌握。


绘制脊型图和马赛克图/SCI论文/科研/研究生/生信分析热点思路的评论 (共 条)

分享到微博请遵守国家法律