这一节我们通过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
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)