前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟小洁老师学习R语言的第六天

跟小洁老师学习R语言的第六天

原创
作者头像
贝诺酯
发布2023-03-16 16:40:48
4940
发布2023-03-16 16:40:48
举报
画图的目的是展示数据
画图的目的是展示数据

常用可视化R包

作图

base

ggplot2(特殊语法:列名不带引号,行末写加号)

颜色:color

大小:size

形状:shape

透明度:alpha

填充颜色:fill(既有边框又有内心的,才需要color和fill两个参数)

映射和手动设置的区别

自行指定映射的具体颜色

代码语言:javascript
复制
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))+
  scale_color_manual(values = c("blue","grey","red"))
分面
代码语言:javascript
复制
#分面
ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species) 
代码语言:javascript
复制
#双分面
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
复制
View(diamonds)
table(diamonds$cut)

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

ggplot(data = diamonds) + 
  stat_count(mapping = aes(x = cut))
统计变换使用场景
代码语言:javascript
复制
#不统计,数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre

ggplot(data = fre) +
  geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")
#count改为prop(比例)
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
位置关系
代码语言: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
复制
# 堆叠直方图
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut,fill=clarity))

#  并列直方图
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
坐标系
代码语言:javascript
复制
#翻转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), 
    width = 1
  ) + 
  theme(aspect.ratio = 1) +
  labs(x = NULL, y = NULL)
bar
bar + coord_flip()
bar + coord_polar()

ggpubr

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

sthda上有大量ggpubr出的图

代码语言:javascript
复制
#1.基础包 略显陈旧 了解一下
plot(iris[,1],iris[,3],col = iris[,5]) 
text(6.5,4, labels = 'hello')

#2.ggplot2 中坚力量,语法有个性
library(ggplot2)
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))

#3.ggpubr 新手友好型 ggplot2简化和美化 褒贬不一
library(ggpubr)
ggscatter(iris,
          x="Sepal.Length",
          y="Petal.Length",
          color="Species")

拼图

par里的mfrow

grid.arrange

cowplot

customLayout

patchwork

导图

经典三段论

代码语言:javascript
复制
pdf("iris_box_ggpubr.pdf")
boxplot(iris[,1]~iris[,5])
text(6.5,4, labels = 'hello')
dev.off()#关闭画板

ggsave

代码语言:javascript
复制
p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")

eoffice—topptx

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

代码语言:javascript
复制
topptx(p,"iris_box_ggpubr.pptx")

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 常用可视化R包
    • 作图
      • base
      • ggplot2(特殊语法:列名不带引号,行末写加号)
      • ggpubr
    • 拼图
      • par里的mfrow
      • grid.arrange
      • cowplot
      • customLayout
      • patchwork
    • 导图
      • 经典三段论
      • ggsave
      • eoffice—topptx
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档