2019-03-10
阅读量:
1283
R中用ggplot画图
收先要载入包
对于初次使用这个包的用户,需要先进行安装ggplot2包
install.packages("ggplot2")
安装后,每次使用前,需要进行载入
library(ggplot2)
ggplot函数的特别之处在于它是分图层的绘图函数,也就是说绘制它的坐标轴和点线是分开的。一个基本的ggplot图像的语句应当是这样的
ggplot(data,aes(x,y))+
geom_xx()+
annotate()+
labs()
第一行语句表示绘制坐标轴,x和y声明的是作为坐标轴的变量,值得一提的是aes(),是美学的意思,在接下来的介绍中我们可以看到,添加到aes()里的东西,会有神奇的效果。
第二行语句表示绘制点、线等其它图形,其中的xx可以是line, point, bar, histogram等,分别表示不同的图形样式。并且,由于ggplot函数是图层形式的,不同的图形可以叠加,如
ggplot(mtcars,aes(mpg,wt))+
geom_line()+
geom_point()
第三行语句annotate()表示添加文本注释;第四行语句表示添加标题以及横轴纵轴的名称,如
ggplot(mtcars,aes(wt,mpg,color=factor(vs)))+geom_point()+
annotate("text",x=4,y=20,label="Yes")+
labs(title="hello",x="xxx",y="yyy")+






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论