美文网首页
R语言学习指南(9) 初探柱状图

R语言学习指南(9) 初探柱状图

作者: R语言数据分析指南 | 来源:发表于2020-12-31 23:49 被阅读0次

这一节我们通过R中iris鸢尾花的数据来探索基础柱状图

整理数据

library(tidyverse)
head(iris)
data <- iris %>% group_by(Species) %>%
  summarise(mean_Sepal.Length=mean(Sepal.Length),
            sd_Sepal.Length=sd(Sepal.Length))
  Species    mean_Sepal.Length sd_Sepal.Length
  <fct>                  <dbl>           <dbl>
1 setosa                  5.01           0.352
2 versicolor              5.94           0.516
3 virginica               6.59           0.636

geom_bar( )与geom_col( )的区别

ggplot(iris, aes(Species))+ geom_bar()

ggplot(iris,aes(Species,Sepal.Length))+ 
  geom_bar(stat="identity")

ggplot(iris,aes(Species,Sepal.Length))+ 
  geom_col()

我们可以看到geom_bar(stat = "identity")geom_col( )做完全一样的事情;geom_bar( )geom_col( )区别关键在于它们在默认情况下如何整合数据,
geom_bar( )默认是计算每个x值的行数,如果为geom_bar( )明确指定stat = "identity"则是告诉ggplot2不自动整合数据,你会提供y值这就与geom_col( )的结果一致,所以当含有y值时直接使用geom_col( )则更加方便
创建第一个条形图

ggplot(data,aes(Species,mean_Sepal.Length))+
geom_col(aes(fill=Species),width=0.5)

一个普普通通的条行图就此诞生了,下面让我们一步一步来改造它

设置从0刻度开始

+ scale_y_continuous(limits = c(0, 9),expand = c(0, 0))+
theme_minimal()

添加刻度线及刻度条

+ theme(axis.line = element_line(color = "#3D4852"),
        axis.ticks = element_line(color = "#3D4852"))

移除垂直网格线

+ theme(panel.grid.major.y = element_line(color = "#DAE1E7"),
        panel.grid.major.x = element_blank())

添加X轴、Y轴、主标题及脚注

+ labs(x = "Species",y = "mean_Sepal.Length",
       title = "The infamous Iris plot",caption = "2020-12-31") 

margin设置主标题与图之间的距离b==bottom

+ theme(plot.title = element_text(size = 20,face = "bold",
                                  margin = margin(b =30)))

设置图边距

+ theme(plot.margin = unit(c(1, 1,1,1), "cm"))
#分别表示上、右、下、左 4方面的边距

调整图中的所有文本

+ theme(axis.text = element_text(size =13,color ="#22292F"),
        axis.title = element_text(size = 12, hjust = 1),
        axis.title.x = element_text(margin = margin(t = 12),size=12,
                                    color="red"),
        axis.title.y = element_text(margin = margin(r = 12)),
        axis.text.y = element_text(margin = margin(r = 5)),
        axis.text.x = element_text(margin = margin(t = 5)),
        plot.caption = element_text(size = 12,face = "italic",
                                    color = "#606F7B",
                                    margin = margin(t =12)))
调整柱子的顺序
+ scale_x_discrete(limits=c("setosa","virginica","versicolor"))

更改填充颜色

+ scale_fill_brewer(palette="Blues")

图例设置

+ theme(legend.position="top")
+ theme(legend.position="bottom")
# Remove legend
+ theme(legend.position="none")

经过上面的分步演示可以看到我们能对图中的任何细节进行微调,下面让我们来看一个完整版

p <- ggplot(data, aes(Species, mean_Sepal.Length)) +
  geom_col(aes(fill=Species),width=0.5) +
  scale_y_continuous(limits = c(0, 9), expand = c(0, 0)) +
  theme_minimal() +
  labs(
    x = "Species", y = "mean_Sepal.Length",
    title = "The infamous Iris plot", caption = "2020-12-31"
  ) +
  theme(
    axis.line = element_line(color = "#3D4852"),
    axis.ticks = element_line(color = "#3D4852"),
    panel.grid.major.y = element_line(color = "#DAE1E7"),
    panel.grid.major.x = element_blank(),
    plot.title = element_text(
      size = 20, face = "bold",
      margin = margin(b = 30)
    ),
    plot.margin = unit(rep(1, 4), "cm"),
    axis.text = element_text(size = 13, color = "#22292F"),
    axis.title = element_text(size = 12, hjust = 1),
    axis.title.x = element_text(margin = margin(t = 12), size = 12,
    color = "red"),
    axis.title.y = element_text(margin = margin(r = 12)),
    axis.text.y = element_text(margin = margin(r = 5)),
    axis.text.x = element_text(margin = margin(t = 5)),
    plot.caption = element_text(
      size = 12, face = "italic",
      color = "#606F7B", margin = margin(t = 12)
    )
  )+
  scale_x_discrete(limits=c("setosa","virginica","versicolor"))+
  scale_fill_brewer(palette="Blues")+
  theme(legend.position="top")
p

添加误差线

pp <- p + geom_errorbar(aes(ymin = mean_Sepal.Length - sd_Sepal.Length,
                    ymax = mean_Sepal.Length + sd_Sepal.Length),
                color = "#22292F",width = .1)
pp

绘制Y轴截断柱状图

p1 <- pp + coord_cartesian(ylim = c(0,5.5))+
  ylab(NULL)+labs(title = NULL)+
  theme(legend.position="no")

p2 <- pp  + coord_cartesian(ylim = c(5.5, 8))+
  theme(axis.text.x = element_blank(), 
        axis.ticks.x = element_blank(), 
        axis.line.x = element_blank())+
  xlab(NULL)+ labs(caption=NULL)+
  theme(legend.position="right")

library(aplot)
p1 %>% insert_top(p2,height=.8)
参考:https://mp.weixin.qq.com/s/hXWEqOBPyhbzTVC6B_khsQ

相关文章

  • R语言学习指南(9) 初探柱状图

    这一节我们通过R中iris鸢尾花的数据来探索基础柱状图 整理数据 geom_bar( )与geom_col( )的...

  • 2020-05-14

    学习小组DAY4笔记-lyq 今天初探R语言 R语言安装 R语言面板在简单了解

  • 异步社区本周半价电子书

    本周半价电子书 《R语言入门经典》 【英】安迪 尼古拉斯著 本书作为R语言的学习指南,详细讲解了R语言的基本概念和...

  • R语言初探

    安装包 包的载入包的载入library()或require(),安装完包后,需要加载才能使用其中的函数,此时括号中...

  • 科研绘图——柱状图

    R语言科研绘图——柱状图 前言 使用R语言绘制能够发表的图片,ggplot2是一个很好的选择。并且一些基于ggpl...

  • 【科研猫·绘图】bar(霸)图绘制之霸气满屏

    怎么做柱状图,怎么做bar图,bar图R语言代码免费分享,R语言做bar图 如果一篇科研论文没有bar图,那它可能...

  • 横向柱状图

    横向柱状图正负值分开,颜色区分 标签: R语言 上面的示意图中我们要注意的问题: 横向柱状图正负值区分颜色赋值画柱...

  • 【R语言】--- 分组柱状图

    基本简介 分组柱状图,又叫聚合柱状图。当需要在同一个轴上显示各个分类下不同的分组时,需要用到分组柱状图,是学术论文...

  • 回头望,来时路

    从开始学习R语言,到今天,已经过去5个多月了,从最初的新鲜,到后来的初探R语言的神奇,到如今竟然已经到达第五关了。...

  • 【R>>barplot】特别的柱状图

    题记:一个简单但富有特色的柱状图 本文为ggplot2绘制一个特别的柱状图(来自公众号:R语言数据分析指南)的学习...

网友评论

      本文标题:R语言学习指南(9) 初探柱状图

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