ggplot-绘制数据分布图

参考文章:
http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/

数据:
ggplot-绘制数据分布图

代码:
1.绘制分布图
library(openxlsx) #读取Excel文件包
library(ggplot2)
df <- read.xlsx(“score.xlsx”,sheet = 1)
a1 <- ggplot(df, aes(x=Score)) +
geom_histogram(aes(y=…density…),binwidth=.1,colour=“black”, fill=“white”) +
geom_density(alpha=.2, fill="#FF6666")+ geom_vline(aes(xintercept=mean(Score, na.rm=T)), color=“red”, linetype=“dashed”, size=1)
geom_histogram:绘制直方图
geom_density:绘制密度曲线

ggplot-绘制数据分布图
2.修饰图片
p1 <- a1 +labs(x=‘Name’,y=‘Density’)+
theme_bw()+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+
theme(axis.text.x= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))+
theme(axis.text.y= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))+
theme(axis.title= element_text(size=15, family=“myFont”, color=“black”, face= “bold”, vjust=0.5, hjust=0.5))
ggplot-绘制数据分布图
3.输出图片
tiff(file=“beautiful.tiff”,res=600,width=3000,height=3000,
compression=“lzw”)
p1
dev.off()

4.描述统计
library(psych)
describe(df)
ggplot-绘制数据分布图