ggplot2基本要素
代码语言:javascript复制ggplot(data = iris)
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
ggplot2默认没有引号,第一行为全局设置,以下分别为分图层。全局设置后一定要由 ,每个分图层可以单独设置映射aes
ggplot2 点图
代码语言:javascript复制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(data = iris)
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
scale_color_manual(values = c("blue","grey","red")) #通过分组设置不同颜色,同时自定义颜色
#区别 fill和color
ggplot(data = iris)
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 17) #17号为实心,只需要color
ggplot(data = iris)
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 2) #2号为空心,color会给边框上色
ggplot(data = iris)
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 24,
fill = "black") #fill会给点图内部填充颜色,填充黑色。
#分面函数facet_wrap
ggplot(data = iris)
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))
facet_wrap(~ Species) #单分面,只分一次
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) #横竖两个分面,需要有纵向分组信息,即重复的若干组数字
全局设置,局部设置(图层)
代码语言:javascript复制ggplot(data = iris)
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
#以上等同于
ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))
geom_smooth()
geom_point()
直方图
代码语言:javascript复制ggplot(data = diamonds)
geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds)
stat_count(mapping = aes(x = cut)) #只有一个参数x,直方图统计的是频数
点图
代码语言:javascript复制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()
geom_jitter() #箱图 抖点图
散点图
代码语言:javascript复制ggsactter(iris,x="Sepal.Length",y="Petal")
my_comparison=list(c("a","b"),c("c","d"))
来自生信技能树笔记