plot(iris[,1],iris[,3],col = iris[,5])
text(6.5,4, labels = 'hello')
基础包绘图函数内容-仅作了解,不常用
# 低级绘图函数
line() #添加线
curve() #添加曲线
abline() #添加给定斜率的线
points() #添加点
segments() #折线
arrows() #箭头
axis() #坐标轴
box() #外框
title() #标题
text() #文字
# 高级绘图函数
plot() #多种图形
hist() #频率直方图
boxplot() #箱线图
stripchart() #点图
barplot() #柱状图
dotchart() #点图
piechart() #饼图
matplot() #数学图形
关闭画板 dev.off()
1.入门级绘图模板:作图数据,横纵坐标-以point为例
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
2.属性设置(颜色、大小、透明度、点的形状,线型等)
2.1 映射:按照数据框的某一列来定义图的某个属性
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species)) #以Species列的分类来自动分配颜色
自行指定映射的颜色
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 手动设置,需要设置为有意义的值--与数据内容无关
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 单位mm
alpha = 0.5, # 透明度 50%
shape = 8) # 点的形状
2.3 区分color和fill两个属性
2.3.1 空心形状和实心形状都用color设置颜色
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 17) #17号,实心的例子
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 2) #2号,空心的例子
2.3.2 既有边框又有内心的,才需要color和fill两个参数
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 24,
fill = "black") #24号,双色的例子
WARNING ①不要修改内置数据,你会忘记修改过 ②保留原始数据,防止可正常运行但错误的代码,不对原始数据进行修改
3.分面
3.1 根据映射分面
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)
4.几何对象-图层
4.1 局部设置: mapping =
在不同的geom中仅对单个图层有效
ggplot(data = iris) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length)) #每个geom_FUNCTION为一个图层,可叠加
4.2 全局设置: mapping =
在ggplot中对所有图层有效
ggplot(data = iris,mapping = aes(x = Sepal.Length,
y = Petal.Length))+
geom_smooth()+
geom_point() #最优写法
5.统计变化--柱状图
#y轴值为默认统计的
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut)) #geom_bar与stat_count相对应,属于ggplot中不同的体系,不用细究
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))
5.1 使用表中数据直接做图,不统计(统计变化使用场景1)
fre = as.data.frame(table(diamonds$cut))
fre
ggplot(data = fre) +
geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity") #手动添加y轴/y值
5.2 将count改为prop(统计变化使用场景2)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = after_stat(prop), group = 1))
#after_stat为统计比例
6.位置关系
6.1 boxplot图加散点
ggplot(data = iris,mapping = aes(x = Species,
y = Sepal.Width,
fill = Species)) +
geom_boxplot()+
geom_jitter()
#注意这里要用 geom_jitter()而不是geom_point()
6.1 position
参数差异所产生的不同条形图,具体可查看帮助文档
第一种: 不指定,默认映射
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
第二种: position = "fill"
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill = clarity),
position = "fill")
第三种: position = "dodge"
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity),
position = "dodge")
7.坐标系
bar <- ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut),
width = 1) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar
坐标系翻转coord_flip()
bar + coord_flip()
极坐标系coord_polar()
★★★完整绘图模板★★★
ggplot(data = <DATA>)+
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>
position = <POSITION>
)+
<COORDINATE_FUNCTION>+
<FACET_FUNCTION>
library(ggpubr) #可调节参数少于ggplot2
ggscatter(iris,
x="Sepal.Length",
y="Petal.Length",
color="Species")
代码可运行却不出图--画板被占用
多次运行
dev.off()
,直到NULL DEVICE为止,再重新运行出图代码,或dev.new()
pdf("iris_box_ggpubr.pdf")
boxplot(iris[,1]~iris[,5])
text(6.5,4, labels = 'hello')
dev.off() # 出现NULL 为结束
p <- ggboxplot(iris, x = "Species",
y = "Sepal.Length",
color = "Species",
shape = "Species",
add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")
详细内容可见小洁老师的前期推文https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ
以上内容均引用自生信技能树
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。