学习小组Day4笔记--apple

今天是开始学习R语言的一天,由于前期已经有一部分了解,R也已经安装完毕,先说说一些tips

  • 前期基础一定要打好,书一定要认真看,推荐《R语言实战》
  • 做好不断报错的准备,其实慢慢掌握来自于不断解决各种报错,所以心态一定摆好,报错是常有的事
  • 在输入代码的过程中,包括各种标点符号都要用英文输入法,不然会报错
  • 电脑的路径设置改成都是英文,前期因为这个吃了不少亏

ggplot2学习

利用ggplot2的内置数据框mpg作为数据

所有的作图基础都基于 ggplot(data =) + (mapping = aes())

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
image.png
  • 不同类型的点显示颜色,以class作为分类区分
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
image.png
  • 设置点的大小
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy, size = class))
image.png
  • 同时设置不同的映射,只需要知道是什么含义就能加,shape指形状,alpha 指透明度
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy, color = class,shape = class, alpha = class))
image.png

在这里会有一个警告信息,自动设置形状只有6个,而这里class有7中,第七种就被移除出去


警告信息
  • 也可以根据自己的喜好改变散点的形状和颜色,下面的代码我们可以看到color = "blue"是不在映射里面的
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
image.png
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy, stroke = 3),shape=21,color = "yellow",fill="red")
image.png

shape选择不同序号,代表不同的形状

  • 空心形状 0-14 color边框
  • 实心形状 15-20 color填充
  • 填充形状 21-24 color边框,和fill填充


    生信星球
  • 单个变量进行单面显示,nrow指定分面后显示几行,ncol指定分面后显示几列,
    需要注意的是
    ~分面依据必须是离散型变量。
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_wrap(~ class, nrow = 2) #分两行展示
image.png
  • 两个变量分面 facet_grid()
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_grid(drv ~ cyl)
image.png
  • 不想在行或列维度中分面,用.代替变量名
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_grid(. ~ cyl)

效果和单个变量分面一样,就是只要1行

ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_wrap(~ cyl, nrow = 1)
image.png
  • 接下来理解不同的几何对象,嗯,就是要画不同的图,上面画的散点图,还可以画线图,柱状图
ggplot(data = mpg) +
  geom_smooth(mapping = aes(x = displ, y = hwy,linetype = drv))
image.png
  • 如果要同时画散点图和线图,这里就是不同的图层叠加,学会一个新名词,局部映射
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy,color = drv,)) +
  geom_smooth(mapping = aes(x = displ, y = hwy,color = drv))
image.png
  • 像上面要分别在散点和线性映射里面分别设置color,就会很麻烦,所以就引入了全局映射
ggplot(data = mpg, mapping = aes(x = displ, y = hwy,color = drv)) + 
  geom_point() + 
  geom_smooth()
image.png

统计变换

这个目前还有待于我进一步理解,大约看懂了,但实际操作是如何应用?

  • 需要好好理解这段,以前不知道这种图怎么做出来,比如在β珠蛋白簇上各个时期珠蛋白所占比例就是用这种方法,或者说不同分组的细胞周期表达模式不同,position="fill",这样设置使得每组堆叠条形具有相同的高度。
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
image.png

加入alpha = 1/2(数字越小越透明),可以设置透明度

  geom_bar(mapping = aes(x = cut, fill = clarity), alpha = 1/2, position = "fill")
image.png
  • 把刚刚散点图的代码换成geom_jitter(),出来的点会变多,因为原本重叠被挡住的点也显示出来了
    这个是geom_point
  geom_point(mapping = aes(x = displ, y = hwy,color=class))
image.png

换成geom_jitter()

ggplot(data = mpg) + 
geom_jitter(mapping = aes(x = displ, y = hwy,color=class))
image.png
  • 一个棒棒的翻转:coord_flip翻转坐标系
    原来的样纸
ggplot(data = mpg, mapping = aes(x = class, y = hwy,color=class)) + 
  geom_boxplot()
image.png

翻转后的样纸,莫名就是觉得高级哈哈

ggplot(data = mpg, mapping = aes(x = class, y = hwy,color=class)) + 
  geom_boxplot()+
  coord_flip()
image.png
  • 把前面的柱形图画成圈圈图,神似葱油饼
ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill") +
  coord_polar(theta = "y")
image.png

所以作图的过程就是不断加上自己想要的图层,不断转化几何图形的过程

本学习内容均来自生信星球

你可能感兴趣的:(学习小组Day4笔记--apple)