一、手动设置和映射以及区别
1、手动设置:需要设置有意义的值
#1)设置颜色
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length),
color = "blue")
#2)十六进制颜色设置:使用snipaste软件可以直接得到颜色指标
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length),
color = "#8dd3c7")
#3)其他指标设置
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length),
size = 3, # 点的大小5mm
alpha = 1.0, # 透明度 50%
shape = 17) # 点的形状
2、区分color和fill两个属性
#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)既有边框又有内心的,才需要color和fill两个参数
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 24,
fill = "black") #24号,双色的例子
3、映射:按照数据框的某一列来定义图的某个属性;此外有两种方法可以自行设置颜色
#1)方法一
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))+
scale_color_manual(values = c("blue","grey","red"))
#2)方法二,颜色选择更多
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))+
scale_color_brewer(palette = "Set1")
display.brewer.all(type = "qual")#查看各类颜色
二、几何对象
#1)局部设置和全局设置
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()
#2)抖动的点图,把重叠的点随机散开
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() #geom_point(position = "jitter")也可以
#3)图层的顺序以及如何对多个图层中的单个进行修改
ggplot(data = iris,mapping = aes(x = Sepal.Width,
y = Species)) +
geom_violin (aes(fill=Species))+
geom_boxplot()+
geom_jitter(aes(shape=Species))
ggsave("iris.png")#保存图片
三、其他
1、导出图的方法
#1)导出为图片或PDF
ggsave("iris.png")
#2)导出文PPT格式,方便再编辑,但太大的图就不行,会卡
library(eoffice)
topptx(p,filename = "iris.pptx")
2、随机取样的函数;随机种子
#随机抽样
sample(1:24,3,replace=T)#该参数可以将取的元素放回整体中
#将每次抽样结果固定
set.seed(12)#随便一个数
sample(1:24,3,replace=T)
3、拼图包
4、代码可以运行但是不出图的原因
5、找现成的代码:画图合辑(小洁老师/其他);学习资料工作目录中
6、画图的正确思维
生信技能树:小洁老师
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。