首页 > 编程语言 > 详细

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

时间:2016-05-12 17:30:56      阅读:236      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!