码迷,mamicode.com
首页 > 编程语言 > 详细

R语言学习笔记——用ggplot2作图

时间:2016-05-12 17:31:38      阅读:714      评论:0      收藏:0      [点我收藏+]

标签:

数据:ggplot2中的diamonds数据集,利用随机抽样取1000个样本,代码如下:

> library(ggplot2)

> data("diamonds")

> head(diamonds)

技术分享

> set.seed(1234)

> small <- diamonds[sample(nrow(diamonds),1000), ]

直方图

> ggplot(small)+geom_histogram(aes(x=price),fill="cornflowerblue",colour="black",binwidth= 1000)

技术分享技术分享


堆叠直方图

用数据中的“cut”列(分为5个档次,用来区分钻石的质量)来作堆叠直方图如下:

>ggplot(small)+geom_histogram(aes(x=price),fill="cornflowerblue",colour="black",binwidth= 1000)

技术分享

技术分享


箱线图

> ggplot(small)+geom_boxplot(aes(x=cut,y=price),fill="cornflowerblue",colour="black")

技术分享

技术分享

柱状图

>ggplot(small)+geom_bar(aes(x=clarity),fill="cornflowerblue",colour="black")

技术分享

技术分享


饼图

>ggplot(small)+geom_bar(aes(x=factor(1),fill=cut),width=1)+coord_polar(theta="y")

技术分享

技术分享

R语言学习笔记——用ggplot2作图

标签:

原文地址:http://blog.csdn.net/qq_24393885/article/details/51367262

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!