R語言學(xué)習(xí)指南(9) 初探柱狀圖

這一節(jié)我們通過R中iris鳶尾花的數(shù)據(jù)來探索基礎(chǔ)柱狀圖

整理數(shù)據(jù)

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( )的區(qū)別

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( )區(qū)別關(guān)鍵在于它們在默認情況下如何整合數(shù)據(jù),
geom_bar( )默認是計算每個x值的行數(shù),如果為geom_bar( )明確指定stat = "identity"則是告訴ggplot2不自動整合數(shù)據(jù),你會提供y值這就與geom_col( )的結(jié)果一致,所以當含有y值時直接使用geom_col( )則更加方便
創(chuàng)建第一個條形圖

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

一個普普通通的條行圖就此誕生了,下面讓我們一步一步來改造它

設(shè)置從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"))

移除垂直網(wǎng)格線

+ 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設(shè)置主標題與圖之間的距離b==bottom

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

設(shè)置圖邊距

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

調(diào)整圖中的所有文本

+ 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)))
調(diào)整柱子的順序
+ scale_x_discrete(limits=c("setosa","virginica","versicolor"))

更改填充顏色

+ scale_fill_brewer(palette="Blues")

圖例設(shè)置

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

經(jīng)過上面的分步演示可以看到我們能對圖中的任何細節(jié)進行微調(diào),下面讓我們來看一個完整版

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容