昨天在做一个简单的频率分布直方图时,想在上方增添一个折线图,但是发现简单的geom_line()
所添加的曲线有点生硬,在想有没有使其平滑的方法,于是google一番发现还是很容易实现的~~
载入数据
## 加载包 library(ggplot2) set.seed(123) data1 <- data.frame(a = runif(170,0.05,1))
数据预处理
将数据切割为8份,计算每个区间的频率值与个体数量,然后通过直方图显示区间频率,线图绘制个体的数量变化。
data1$range <- cut(data1$a,breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8), labels = c('0-0.1','0.1-0.2','0.2-0.3','0.3-0.4','0.4-0.5','0.5-0.6','0.6-0.7','0.7-0.8'),right = FALSE) new_data1 <- as.data.frame(table(data1$range)) %>% mutate(prob = (Freq/sum(Freq))*100) %>% mutate(label = paste(round(prob,1)))
直方图+线图
这里简单通过scale_y_continuous
中的sec.axis
参数添加次坐标轴,对应线图的值。
plot <- ggplot(new_data1,aes(Var1,prob))+geom_col(fill = 'black',width = .5)+ theme_classic()+ #theme(axis.text.x = element_text(face="bold", size=12),axis.text.y = element_text(face="bold", size=12))+ scale_y_continuous(expand=c(0,0), limits=c(0, 35), breaks=seq(0, 35, by=5),sec.axis = sec_axis(~.+5,name="Number"))+ geom_text(aes(Var1,prob+2,label = label),size = 4,fontface = "bold",hjust = 0.5,vjust = 1)+ labs(x = 'data', y = 'Frequences')+ theme(axis.line.x=element_line(size=.5, colour="black"), axis.line.y=element_line(size=.5, colour="black"), axis.title.x=element_text(colour='black', size=16,face = "bold"), axis.title.y=element_text(colour='black', size=16,face = "bold"), axis.ticks = element_line(color = "black"), axis.ticks.length = unit(0.2,"lines"), #控制坐标轴深处标签 axis.line = element_line(colour = "black",size = 14), axis.text.y = element_text(colour='black',size=10), #panel.border = element_blank(), axis.text.x = element_text(angle = 45,colour = "black",size = 10,vjust = 0.8,hjust = 0.8)) plot1 <- plot + geom_point(aes(Var1,Freq),color="black", size=1)+ geom_line(aes(Var1,Freq),group = 1,color = 'black',linetype = "dashed",size = .5) plot1
直方图+平滑折线
解决方法是使用geom_xspline
函数,需要安装ggalt
包加载。
注意:因为geom_xspline函数绘制时候必须两列数据为数值型,像天数日期这种,因此当我们为因子型的数据时候,不妨先将标签转换为阿拉伯数字,然后使用as.numeric
进行转化即可,例如下方代码:
data1$range <- cut(data1$a,breaks = c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8), labels = c('1','2','3','4','5','6','7','8'),right = FALSE) new_data1 <- as.data.frame(table(data1$range)) %>% mutate(prob = (Freq/sum(Freq))*100) %>% mutate(label = paste(round(prob,1))) new_data1$Var1 <- as.numeric(new_data1$Var1)
作图
plot <- ggplot(new_data1,aes(Var1,prob))+geom_col(fill = 'black',width = .5)+ theme_classic()+ #theme(axis.text.x = element_text(face="bold", size=12),axis.text.y = element_text(face="bold", size=12))+ scale_y_continuous(expand=c(0,0), limits=c(0, 35), breaks=seq(0, 35, by=5),sec.axis = sec_axis(~.+5,name="Number"))+ geom_text(aes(Var1,prob+2,label = label),size = 4,fontface = "bold",hjust = 0.5,vjust = 1)+ labs(x = 'data', y = 'Frequences')+ theme(axis.line.x=element_line(size=.5, colour="black"), axis.line.y=element_line(size=.5, colour="black"), axis.title.x=element_text(colour='black', size=16,face = "bold"), axis.title.y=element_text(colour='black', size=16,face = "bold"), axis.ticks = element_line(color = "black"), axis.ticks.length = unit(0.2,"lines"), #控制坐标轴深处标签 axis.line = element_line(colour = "black",size = 14), axis.text.y = element_text(colour='black',size=10), #panel.border = element_blank(), axis.text.x = element_text(angle = 45,colour = "black",size = 10,vjust = 0.8,hjust = 0.8)) plot2 <- plot + geom_point(aes(Var1,Freq),color="black", size=2)+ geom_xspline(aes(Var1,Freq),spline_shape = -0.4,size=1,col='black',linetype = 'dashed')+ scale_x_discrete(limits = c('0-0.1','0.1-0.2','0.2-0.3','0.3-0.4','0.4-0.5','0.5-0.6','0.6-0.7','0.7-0.8')) plot2
最后关于图形上X轴的标签,我们再通过scale_x_discrete
手动改变过来即可。哈哈,这样看着线条就舒服了。