這一節(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)鍵在于它們在默認(rèn)情況下如何整合數(shù)據(jù),
geom_bar( )
默認(rèn)是計算每個x值的行數(shù),如果為geom_bar( )
明確指定stat = "identity"
則是告訴ggplot2不自動整合數(shù)據(jù)卸勺,你會提供y值這就與geom_col( )
的結(jié)果一致,所以當(dāng)含有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軸温技、主標(biāo)題及腳注
+ labs(x = "Species",y = "mean_Sepal.Length",
title = "The infamous Iris plot",caption = "2020-12-31")
margin設(shè)置主標(biāo)題與圖之間的距離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)過上面的分步演示可以看到我們能對圖中的任何細(xì)節(jié)進(jìn)行微調(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)