03-09笔记绘图

常用绘图R包

1615258161(1).png

1.基础包 略显陈旧 了解一下

plot(iris[,1],iris[,3],col = iris[,5]) 
text(6.5,4, labels = 'hello')

boxplot(iris[,1]~iris[,5])

dev.off()

2.ggplot2 中坚力量 学起来有点难

test = iris
if(!require(ggplot2))install.packages('ggplot2')
library(ggplot2)
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))

3.ggpubr 江湖救急 ggplot2简化和美化 褒贬不一

if(!require(ggpubr))install.packages('ggpubr')
library(ggpubr)

ggscatter(iris,
          x="Sepal.Length",
          y="Petal.Length",
          color="Species")

STHDA美图中心:www.sthda.com

1615270274(1).jpg

1615270785(1).jpg

1.入门级绘图模板:作图数据,横纵坐标

library(ggplot2)
test = iris
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length))

2.属性设置(颜色、大小、透明度、点的形状,线型等)

2.1 手动设置,需要设置为有意义的值
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy), 
             color = "blue")#目前手动只能设置1种颜色,注意color在aes()外!

ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy,color=class), 
             size = 5,     # 点的大小5mm
             alpha = 0.5,  # 透明度 50%
             shape = 8)  # 点的形状
show_point_shapes() + 
    theme_classic()#显示点的形状(ggpubr包内的函数)
image.png
2.2 映射:按照数据框的某一列来定义图的某个属性
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))
Q1 能不能自行指定映射的具体颜色?
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))+
  scale_color_manual(values = c("blue","grey","red"))

十六进制颜色

Q2 区分color和fill两个属性

Q2-1 空心形状和实心形状都用color设置颜色

ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 17) #17号,实心的例子
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 2) #2号,空心的例子

Q2-2 既有边框又有内心的,才需要color和fill两个参数

ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 24,
             fill = "black") #22号,双色的例子

3.分面

ggplot(data = test) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species) 
双分面
test$Group = sample(letters[1:5],150,replace = T)#新增了1列
ggplot(data = test) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_grid(Group ~ Species) #前行后列

练习6-1

示例数据:ggplot2中的数据集mpg

liberary(ggplot2)
a <- mpg
mpg#直接显示ggplot2显示也很优化

1.分别以mpg的displ和hwy两列作为横纵坐标,画点图。

ggplot(data = mpg) + 
  geom_point(mapping = aes(x =displ, y =hwy))

2.尝试修改颜色或大小,从mpg数据框中任选可以用来分类的列。

length(unique(mpg$manufacturer))#看某1列有几个取值
length(unique(mpg$displ))
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy,color=displ), 
             size =4,     # 点的大小5mm
             alpha = 0.8,  # 透明度 50%
             shape = 18)  # 点的形状

分类变量和连续变量 颜色分配方式不同,连续变量的调色自行进一步搜索学习函数

3.根据class列来分面

ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy,color=fl)) + 
  facet_wrap(~ class)

4.根据drv和cyl两个变量来分面

ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy,color=fl)) + 
  facet_grid(drv~ cyl)

配色方案可以搜索教程

4.几何对象

局部设置和全局设置
ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))+
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length))

ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
  geom_smooth()+
  geom_point()

练习6-2

1.尝试写出下图的代码
数据是iris
X轴是Species
y轴是Sepal.Width
图是箱线图,试一试搜一搜,ggplot2箱线图函数是哪个

ggplot(data=iris,mapping = aes(x=Species,y=Sepal.Width))+
  geom_boxplot()+
  geom_point()
  1. 尝试在此图上叠加点图,
    能发现什么问题?

3.用下列代码作图,观察结果

ggplot(test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
  geom_point()+
  geom_smooth(color = "black")

请问,当局部设置和全局设置冲突,谁说了算?局部!

5.统计变换-直方图

View(diamonds)
table(diamonds$cut)

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut))

ggplot(data = diamonds) + 
  stat_count(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")#指定Y轴数据
5.2count改为prop
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))#Y轴按各组比例显示

6.位置关系

6.1抖动的点图
ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_point()

ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_jitter()

补充dotplot,点不重合,也不奔放

ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_dotplot(binaxis = "y",binwidth = .5,stackdir = "center")
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.坐标系

横纵坐标翻转coord_flip()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
  geom_boxplot() +
  coord_flip()

极坐标系coord_polar()

bar <- ggplot(data = diamonds) + 
  geom_bar(
    mapping = aes(x = cut, fill = cut), 
    show.legend = FALSE,
    width = 1
  ) + 
  theme(aspect.ratio = 1) +
  labs(x = NULL, y = NULL)
bar + coord_flip()
bar + coord_polar()
1615274604(1).jpg
#老师简化版
colnames(iris)#显示列名,直接复制。。
ggplot(data = iris,aes(Sepal.Width,Species))+ #简化函数
          geom_violin(aes(fill=Species))+ #代码先后顺序,决定图层上下,不能直接写Specie,因为是映射不是violin单独参数
          geom_boxplot()+
          geom_point()+
          geom_jitter(aes(shape=Species))+
          theme_bw()#主题背景选择

ggpubr 搜代码直接用,基本不需要系统学习

sthda上有大量ggpubr出的图
library(ggpubr)
ggscatter(iris,x="Sepal.Length",
          y="Petal.Length",
          color="Species")

p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
p
my_comparisons <- list( c("setosa", "versicolor"), 
                        c("setosa", "virginica"), 
                        c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
  stat_compare_means(label.y = 9) #箱线图加了组间比较!
image.png

图片保存的三种方法

1.基础包作图的保存

#三段论:保存格式+代码+关闭图片
pdf("iris_box_ggpubr.pdf")
boxplot(iris[,1]~iris[,5])
text(6.5,4, labels = 'hello')
dev.off()

2.ggplot系列图(包括ggpubr)通用的简便保存 ggsave

p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")

3.eoffice包 导出为ppt,全部元素都是可编辑模式

library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")

可探索export包

拼图

R包par基础包 patchwork 兼容
可探索入门
https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ

小洁老师//www.greatytc.com/nb/35523479

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容