模板:
ggplot(data = <DATA>)+
<GEOM_FUNCTION>(mapping =aes(<MAPPINGS>))
注意:
低级绘图函数单独使用会报错,需依附于高级绘图函数。
ggplot2特殊语法:列名不带引号,行末写加号!
fill管实心,color管边框
library(ggplot2)
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length),
size = 1, # 点的大小5mm
alpha = 0.5, # 透明度 50%
shape = 8, # 点的形状
color = "blue")#这里color是geom_point的参数,即:把图形设置为一个或n个颜色,与数据内容无关
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))+#这里的color为aes的参数,紧跟x,y出现,是数据框列名
scale_color_manual(values =c("blue","grey","red")) #自行指定映射的颜色
分面的值必须有重复值
+facet_wrap(~ xxx) ,xxx为数据框的一列,分面的依据
#分一面
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species)
+facet_grid(xxx ~ yyy)
#双分面
dat = iris
#sample()取值,replace表示是否放回
dat$Group = sample(letters[1:5],150,replace = T)#结果是新增一列group内容为随机取的1:5中一个
head(dat)
ggplot(data = dat) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_grid(Group ~ Species)
#局部设置
#全局设置
ggplot(data = iris,mapping = aes(x = Sepal.Width,
y = Species)) +
geom_violin(aes(fill = Species))+
geom_boxplot()+
geom_jitter(aes(shape=Species))
#统计变换-直方图
head(diamonds)
table(diamonds$cut)
##
## Fair Good Very Good Premium Ideal
## 1610 4906 12082 13791 21551
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
+geom_bar(mapping = aes(x = , y = ), stat = "identity")
+geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
翻转:+coord_flip()
极坐标系:+ coord_polar()
当有分组比较需求时,可用ggpubr,其余用ggplot2就够了
library(ggpubr)
my_comparisons <- list(c("setosa","versicolor"),
c("setosa","virginica"),
c("versicolor","virginica"))
ggboxplot(iris,
x = "Species",
y= "Sepal.Length",
add = "jitter")+
stat_compare_means(comparisons = my_comparisons)+
stat_compare_means(label.y = 9)
ggsave("name.png")
ggsave(p, filename = "name.png")
保存的格式和文件名:pdf("test.pdf")
作图代码:...
关闭画板:dev.off
library(eoffice)
topptx(p,"p.pptx")
library(patchwork)
p1+p2
引用自生信技能树马拉松课程小洁老师授课内容:R语言基础03
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。