大数跨境
0
0

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

R语言学习指南(9) 初探柱状图 R语言数据分析指南
2021-01-01
2
导读:柱状图也许是数据分析中最常见的一种图形了,这一节我们通过R中iris鸢尾花的数据来探索基础柱状图整理数据li

柱状图也许是数据分析中最常见的一种图形了,这一节我们通过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(09),expand = c(00))+
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(11,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(09), expand = c(00)) +
  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(14), "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.58))+
  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)

下一节继续展示如何在图上添加各种类型统计学信息

【声明】内容源于网络
0
0
R语言数据分析指南
R语言重症爱好者,喜欢绘制各种精美的图表,喜欢的小伙伴可以关注我,跟我一起学习
内容 1180
粉丝 0
R语言数据分析指南 R语言重症爱好者,喜欢绘制各种精美的图表,喜欢的小伙伴可以关注我,跟我一起学习
总阅读410
粉丝0
内容1.2k