所用R 包 ggplot2
數(shù)據(jù):

image.png
最終效果:

image.png
代碼
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"),
axis.title = element_text(face = 'bold',color = 'black',size = 13,hjust = 0.5,family = "Times New Roman"),legend.position = 'right',legend.text = element_text(face = 'bold',color = 'black',size = 10,hjust = 0.5,family = "Times New Roman"),
legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"))+
scale_x_continuous(expand = c(0, 0),limits = c(0,1), breaks = seq(0,1,0.1)) +scale_y_continuous(expand = c(0, 0))
分步說明:
一、指定數(shù)據(jù)集
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))
告訴ggplot要以phynotype這個表格畫圖,把RSRDS列作為x軸;
二、畫圖類型為直方圖
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS))
圖形類別為直方圖

image.png
三、添加直方圖邊框,讓條之間分開
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),
fill="springgreen4", color="white",size=0.2, alpha=0.7)
...density...:以密度為y軸,如果以數(shù)量,就改為 ...count...;
fill="springgreen4"填充顏色為springgreen4;
color="white"邊框顏色為白色;
size=0.2:邊框粗0.2;
alpha=0.7:透明度為0.7

image.png
四、添加密度曲線
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)
如果計數(shù),就用...count...代替 ...density...

image.png
五:去除背景
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()

image.png
六:調整刻度標簽:
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text= element_text(face = 'bold',color = 'red',size = 12,hjust = 0.5,family = "Times New Roman"))

image.png
同時調整xy軸,如果單獨調整一個,則用
theme(axis.text.x/y)
加粗,紅色,字號12,水平調整0.5,字體time new roman.
七、調整軸標簽
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text= element_text(face = 'bold',color = 'red',size = 12,hjust = 0.5,family = "Times New Roman")
,axis.title = element_text(face = 'bold',color = 'blue',size = 13,hjust = 0.5,family = "Times New Roman")))

image.png
八、調整圖例 (本圖無圖例)
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"),
axis.title = element_text(face = 'bold',color = 'black',size = 13,hjust = 0.5,family = "Times New Roman"),
legend.position = 'right',legend.text = element_text(face = 'bold',color = 'black',size = 10,hjust = 0.5,family = "Times New Roman"),
legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"))

image.png
九、將圖畫在原點上
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"),
axis.title = element_text(face = 'bold',color = 'black',size = 13,hjust = 0.5,family = "Times New Roman"),legend.position = 'right',legend.text = element_text(face = 'bold',color = 'black',size = 10,hjust = 0.5,family = "Times New Roman"),legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"))+
scale_x_continuous(expand = c(0, 0))+scale_y_continuous(expand = c(0, 0))

image.png
scale_x_continuous(expand = c(0, 0))+scale_y_continuous(expand = c(0, 0))
十、更改坐標軸顯示范圍及間隔
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"),
axis.title = element_text(face = 'bold',color = 'black',size = 13,hjust = 0.5,family = "Times New Roman"),legend.position = 'right',legend.text = element_text(face = 'bold',color = 'black',size = 10,hjust = 0.5,family = "Times New Roman"),legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"))+
scale_x_continuous(expand = c(0, 0),limits = c(0,1), breaks = seq(0,1,0.1))+scale_y_continuous(expand = c(0, 0),limits = c(0,10), breaks = seq(0,10,1))

image.png
scale_x_continuous(expand = c(0, 0),limits = c(0,1), breaks = seq(0,1,0.1))
expand擴大顯示范圍,limits坐標軸范圍,breaks最小,最大,間隔
十一、坐標軸標題
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"),
axis.title = element_text(face = 'bold',color = 'black',size = 13,hjust = 0.5,family = "Times New Roman"),legend.position = 'right',legend.text = element_text(face = 'bold',color = 'black',size = 10,hjust = 0.5,family = "Times New Roman"),legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"))+
scale_x_continuous(expand = c(0, 0),limits = c(0,1), breaks = seq(0,1,0.1))+scale_y_continuous(expand = c(0, 0),limits = c(0,10), breaks = seq(0,10,1))+xlab(NULL)+ylab("Freqency")

image.png
xlab(NULL)+ylab("Freqency")
x軸標題沒有,y軸標題為Freqency
十二、另一種改變坐標軸范圍的方式
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"),
axis.title = element_text(face = 'bold',color = 'black',size = 13,hjust = 0.5,family = "Times New Roman"),legend.position = 'right',legend.text = element_text(face = 'bold',color = 'black',size = 10,hjust = 0.5,family = "Times New Roman"),legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"))+
scale_x_continuous(expand = c(0, 0),limits = c(0,1), breaks = seq(0,1,0.1))+scale_y_continuous(expand = c(0, 0),limits = c(0,10), breaks = seq(0,10,1))+xlab(NULL)+ylab("Freqency")+coord_cartesian(ylim = c(0,15))

image.png
coord_cartesian(ylim = c(0,15)) y軸范圍0至15
十三、調整坐標軸線
RSRDS<-ggplot(data=phynotype,aes(x=RSRDS))+
geom_histogram(data=phynotype,aes(x=RSRDS,..density..),fill="springgreen4", color="white",size=0.2, alpha=0.7)+
geom_density(data=phynotype,aes(x=RSRDS,..density..),size=0.4)+
theme_classic()+
theme(axis.text = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"),
axis.title = element_text(face = 'bold',color = 'black',size = 13,hjust = 0.5,family = "Times New Roman"),legend.position = 'right',legend.text = element_text(face = 'bold',color = 'black',size = 10,hjust = 0.5,family = "Times New Roman"),legend.title = element_text(face = 'bold',color = 'black',size = 12,hjust = 0.5,family = "Times New Roman"))+
scale_x_continuous(expand = c(0, 0),limits = c(0,1), breaks = seq(0,1,0.1))+
scale_y_continuous(expand = c(0, 0),limits = c(0,10), breaks = seq(0,10,1))+
xlab(NULL)+ylab("Freqency")+
coord_cartesian(ylim = c(0,15))+
theme(axis.line=element_line(linetype=1,color="green",size=0.5))

image.png
theme(axis.line=element_line(linetype=1,color="green",size=0.5))