library(ggplot2)
library(gridExtra)
iris=read.csv("D:/Rdata/Windows/data/chap3/Iris.csv")
##ggplot(data,aes(x=...,y=...))#初始化一个ggplot绘图对象
##theme_**()控制图像非数据显示的内容
##geom_**()绘图方式
windowsFonts(STKaiti=windowsFont("华文楷体"))#调用windows字体
##散点图
p1=ggplot(iris,aes(x=PetalLengthCm,y=PetalWidthCm))+
theme_bw(base_size =20,base_family="STKaiti")+
geom_point(aes(color = Species))+labs(title = "散点图")
##小提琴图
p2=ggplot(iris,aes(x= Species,y = SepalLengthCm))+
theme_gray(base_family = "STKaiti",base_size = 15)+
geom_violin(aes(fill=Species),show.legend = F)+
labs(title = "小提琴图")+
theme(plot.title = element_text(hjust=0.9))
##密度函数
p3=ggplot(iris,aes(SepalWidthCm))+
theme_minimal(base_family = "STKaiti",base_size = 10)+
geom_density(aes(color=Species,fill=Species),alpha=0.5)+
labs(title = "密度曲线")+
theme(plot.title = element_text(hjust = 0.5),legend.position = c(0.8,0.8))
##等高线图
p4=ggplot(iris,aes(x=SepalLengthCm,y=SepalWidthCm))+
theme_classic(base_family = "STKaiti",base_rect_size = 5)+
geom_point(shape=17)+
geom_density2d(linemitre = 5)+
theme(plot.title = element_text(hjust = 0.2))+
ggtitle("二维密度曲线")
##将4个图像放进一个图像中
grid.arrange(p1,p2,p3,p4,nrow=2)
R语言ggplot画图初步1
最新推荐文章于 2023-10-12 13:25:23 发布