生信技能树 R语言入门 ggplot2画图

您所在的位置:网站首页 推文横图尺寸 生信技能树 R语言入门 ggplot2画图

生信技能树 R语言入门 ggplot2画图

2023-04-07 12:10| 来源: 网络整理| 查看: 265

ggplot2基操1.入门级绘图模板:作图数据+横纵坐标ggplot(data = iris) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))2.属性设置(颜色、大小、透明度、点的形状,线型等)2.1 手动设置,需要设置为有意义的值

颜色:

library(ggplot2) ggplot(data = iris) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length), color = "blue") #输入参数时,列名不需要加引号

大小、透明度、形状:

ggplot(data = iris) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length), size = 5, # 点的大小5mm alpha = 0.5, # 透明度 50% shape = 8) # 点的形状

ggplot常用的五个参数:color, size, shape, alpha, fill;shape有25中形状

2.2 映射:按照数据框的某一列来定义图的某个属性ggplot(data = iris)+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length, color = Species))+ scale_color_manual(values = c("blue","grey","red"))2.2.2区分color和fill两个属性:既有边框又有内心的,才需要color和fill两个参数ggplot(data = iris)+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length, color = Species), shape = 24, fill = "black") #24号,双色的例子ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, color = Species))+ geom_boxplot()#变成实心的颜色:将color改成fill ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, fill = Species))+ geom_boxplot()3.分面ggplot(data = iris) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + facet_wrap(~ Species) 3.2 双分面dat = iris dat$Group = sample(letters[1:5],150,replace = T) ggplot(data = dat) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + facet_grid(Group ~ Species)

#sample(x, size, replace = FALSE, prob = NULL) #size是取几个数,replace是是否能重复取

4.几何对象 ——局部设置和全局设置ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+ geom_smooth()+ geom_point()

ggplot内是全局设置,geom里是局部设置

5.统计变换-直方图View(diamonds) table(diamonds$cut) ## ## Fair Good Very Good Premium Ideal ## 1610 4906 12082 13791 21551ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut))5.1 不统计,数据直接做图fre = as.data.frame(table(diamonds$cut)) fre ggplot(data = fre) + geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")5.2 count改为propggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))6.位置关系6.1抖动的点图ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, fill = Species)) + geom_boxplot()+ geom_point()

所有点在一条线上,分开各个点使其显示更具象

ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, fill = Species))+ geom_boxplot(width=1)+ geom_jitter(size=0.6)6.2堆叠直方图ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut,fill=clarity))6.3 并列直方图ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")7. 坐标系7.1 翻转coord_flip()ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot() + coord_flip()7.2 极坐标系coord_polar()bar


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3