03-08

作者: Everlyn | 来源:发表于2021-05-14 08:27 被阅读0次

06 R语言作图

图就是数据,数据就是图

常用可视化R包

作图:base,ggplot2, ggpubr;
拼图:patchwork;
导出:pdf()等三段论, ggsave, eoffice-topptx。


image.png

低级绘图参数作用是在高级绘图函数添砖加瓦,而绘图参数存在于函数内部,在没有设定值时使用默认值。

> plot(iris[,1],iris[,3],col = iris[,5]) #plot是高级绘图函数
> text(6.5,4, labels = 'hello')#text是低级绘图函数,加标注的
#label为绘图参数

plot最古老最丑
ggplot背景为灰色,图注在右侧
ggpubr简化/美化,图注在上方

ggplot2语法

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

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

2.手动设置与映射

2.1 手动设置,需要设置为有意义的值

颜色color 大小 size 形状shape 透明度 alpha 填充颜色fill

> ggplot(data = mpg) + 
+   geom_point(mapping = aes(x = displ, y = hwy), 
+              color = "blue")#color是geompoint的参数,是具体颜色
> ggplot(data = mpg) + 
+   geom_point(mapping = aes(x = displ, y = hwy), 
+              size = 5,     # 点的大小5mm
+              alpha = 0.5,  # 透明度 50%
+              shape = 8)  # 点的形状可以为数字或手动敲出来的形状
> #手动设置只能设置出1种颜色

2.2 映射:按照数据框的某一列来定义图的某个属性

> ggplot(data = test)+
+   geom_point(mapping = aes(x = Sepal.Length,
+                            y = Petal.Length,
+                            color = Species))#color是aes的参数,是列名

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) #按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)#按Group分行,按Species分列
 #练习6-1
> # 示例数据:ggplot2中的数据集mpg
> # 1.分别以mpg的displ和hwy两列作为横纵坐标,画点图。
> library(ggplot2)
> test=mpg
> ggplot(data = mpg) + 
+   geom_point(mapping = aes(x = displ, y = hwy))
> # 2.尝试修改颜色或大小,从mpg数据框中任选可以用来分类的列。
> ggplot(data = mpg) + 
+   geom_point(mapping = aes(x = displ, 
+                            y = hwy,
+                            color=class))
> ggplot(data = mpg) + 
+   geom_point(mapping = aes(x = displ, 
+                            y = hwy,
+                            color=displ),size=1)
> # 3.根据class列来分面
> ggplot(data = mpg) + 
+   geom_point(mapping = aes(x = displ, y = hwy,color=displ),size=1)+facet_wrap(~class)
> # 4.根据drv和cyl两个变量来分面
> ggplot(data = mpg) + 
+   geom_point(mapping = aes(x = displ, 
+                            y = hwy,
+                            color=displ),size=1)+
+   facet_grid(drv~cyl)

4.几何对象(约等于图层)

image.png
image.png
> ggplot(data = test) + 
+   geom_smooth(mapping = aes(x = Sepal.Length, 
+                           y = Petal.Length))+
+   geom_point(mapping = aes(x = Sepal.Length, 
+                            y = Petal.Length))
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
> ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
+   geom_smooth()+
+   geom_point()
`geom_smooth()` using method = 'loess' and formula 'y ~ x'

5.统计变换

#5.统计变换-直方图
> View(diamonds)
> table(diamonds$cut)

     Fair      Good Very Good   Premium     Ideal 
     1610      4906     12082     13791     21551 
> 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
       Var1  Freq
1      Fair  1610
2      Good  4906
3 Very Good 12082
4   Premium 13791
5     Ideal 21551
> ggplot(data = fre) +
+   geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")

5.2 count改为prop

> ggplot(data = diamonds) + 
+   geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

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()
> 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()

相关文章

  • 03-08

    今天学习的做计算机,看起来很简单,做起来真的很麻烦。

  • 03-08

    06 R语言作图 图就是数据,数据就是图 常用可视化R包 作图:base,ggplot2, ggpubr;拼图:p...

  • 2018-04-02

    08:02 到公司 08:03-08:05 整理桌面卫生 08:06-08:12 整理处理架构师工作内容和工作技能...

  • 2019创业金句

    创业卡片,诚毅每日分享,2019·03-08第82期 成功的路上,并不拥挤。因为有决心有毅力的人,真的不多。 优秀...

  • 03-08 赞赏

    赞赏钱宝宝源源不断进来。 今天38节,儿子给了我20.5元。抢到红包4.16、72.27、1.03、3.8、15....

  • 看书/《被讨厌的勇气》

    2020-03-07/03-08 昨日由于写推文已经没有多余的精力日更日记了。 忙着写女神节的广告推文。 对了,昨...

  • 03-08 聊聊近况

    此外,幸福和不幸是共生关系,两者紧密相联。因此,它们始终是平衡的:“有痛苦的时候,出于平衡的考虑,就必然会产生快乐...

  • 03-08真假康乃馨

    1.女人如花。 三八节一早就去参加了一个读书活动,这是我爱自己的方式之一。 在钱塘江边优雅的书院里,听完相声月,听...

  • 最高检:检察机关坚决监督纠正重大冤错案件

    最高检:检察机关坚决监督纠正重大冤错案件 新浪新闻03-08 11:44 正义网北京3月8日电(记者 于潇)由最高...

  • 03-08碎碎念

    心如明镜 想问大家一个问题,你们是怎样和家人相处的呢?可以不答,因为这不是重点,下一问才是重点。 请问你觉得『自己...

网友评论

      本文标题:03-08

      本文链接:https://www.haomeiwen.com/subject/xdpadltx.html