ggplot,geom_histogram画直方图

所用R 包 ggplot2

数据:

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))

分步说明:

一、指定数据集

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轴,如果以数量,就改为 ...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)

如果计数,就用...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))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容