关于ggplot2包(一)

关于ggplot2包(一)

ggplot2基本要素

  • 数据(Data)和映射(Mapping)
  • 几何对象(Geometric)
  • 标尺(Scale)
  • 统计变换(Statistics)
  • 坐标系统(Coordinate)
  • 图层(Layer)
  • 分面(Facet)
  • 主题(Theme)

数据(Data)和映射(Mapping)

下面用diamonds的数据为例,由于数据很大,随机选取一个子集进行画图

> library(ggplot2)
> data(diamonds)
> set.seed(42)#设定生成随机数的种子,使结果具有重复性
> small<-diamonds[sample(nrow(diamonds),1000),]#抽样
> head(small)
carat cut color clarity depth table price x y z
49345 0.71 Very Good H SI1 62.5 60 2096 5.68 5.75 3.57
50545 0.79 Premium H SI1 61.8 59 2275 5.97 5.91 3.67
15434 1.03 Ideal F SI1 62.4 57 6178 6.48 6.44 4.03
44792 0.50 Ideal E VS2 62.2 54 1624 5.08 5.11 3.17
34614 0.27 Ideal E VS1 61.6 56 470 4.14 4.17 2.56
27998 0.30 Premium E VS2 61.7 58 658 4.32 4.34 2.67

summary一下

> summary(small)
carat cut color clarity depth
Min. :0.2200 Fair : 28 D:121 SI1 :258 Min. :55.20
1st Qu.:0.4000 Good : 88 E:186 VS2 :231 1st Qu.:61.00
Median :0.7100 Very Good:227 F:164 SI2 :175 Median :61.80
Mean :0.8187 Premium :257 G:216 VS1 :141 Mean :61.71
3rd Qu.:1.0700 Ideal :400 H:154 VVS2 : 91 3rd Qu.:62.50
Max. :2.6600 I:106 VVS1 : 67 Max. :72.20
J: 53 (Other): 37
table price x y
Min. :50.10 Min. : 342.0 Min. :3.850 Min. :3.840
1st Qu.:56.00 1st Qu.: 989.5 1st Qu.:4.740 1st Qu.:4.758
Median :57.00 Median : 2595.0 Median :5.750 Median :5.775

Mean :57.43 Mean : 4110.5 Mean :5.787 Mean :5.791
3rd Qu.:59.00 3rd Qu.: 5495.2 3rd Qu.:6.600 3rd Qu.:6.610
Max. :65.00 Max. :18795.0 Max. :8.830 Max. :8.870

z
Min. :2.330
1st Qu.:2.920
Median :3.550
Mean :3.572
3rd Qu.:4.070
Max. :5.580

以克拉(carat)数为X轴变量,价格(price)为Y轴变量

> p<-ggplot(data=small,mapping=aes(x=carat,y=price))#将数据映射到XY坐标轴上

下面,画出散点图

> p+geom_point()



如果想把切工(cut)映射到形状属性:

> p<-ggplot(data=small,mapping=aes(x=carat,y=price,shape=cut))
> p+geom_point()



如果想再将颜色(color)映射颜色属性:

> p<-ggplot(data=small,mapping=aes(x=carat,y=price,shape=cut,colour=color))
> p+geom_point()

几何对象(Geometric)

在上面的例子,各种属性映射都由ggplot函数执行,只需要加一个图层,使用geom_point()告诉ggplot要画散点图,于是所有的属性都映射到散点上。

再如geom_histogram用于直方图,geom_bar用于画柱状图,geom_boxplot用于画箱式图等。

上图,也可以用下面代码,来画

> p<-ggplot(small)
> p+geom_point(aes(x=carat,y=price,shape=cut,colour=color))

直方图

>ggplot(small)+geom_histogram(aes(x=price))



同样可以根据另外的变量给它填充颜色

>ggplot(small)+geom_histogram(aes(x=price,fill=cut))



同样,可以将它们分开

>ggplot(small)+geom_histogram(aes(x=price, fill=cut), position="dodge")



还可以按照相对比例来画,

>ggplot(small)+geom_histogram(aes(x=price, fill=cut), position="fill")

柱状图

> ggplot(small)+geom_bar(aes(x=clarity))



通过stat参数,可以让geom_bar按指定高度画图,

> ggplot()+geom_bar(aes(x=c(LETTERS[1:3]),y=1:3), stat="identity")

密度函数图

> ggplot(small)+geom_density(aes(x=price, colour=cut))

> ggplot(small)+geom_density(aes(x=price,fill=clarity))



colour参数指定的是颜色,fill是往曲线下面填充颜色

箱式图

> ggplot(small)+geom_boxplot(aes(x=cut, y=price,fill=color))

下面是各种geom_xxx函数

geom_abline    geom_area  

geom_bar geom_bin2d

geom_blank geom_boxplot

geom_contour geom_crossbar

geom_density geom_density2d

geom_dotplot geom_errorbar

geom_errorbarh geom_freqpoly

geom_hex geom_histogram

geom_hline geom_jitter

geom_line geom_linerange

geom_map geom_path

geom_point geom_pointrange

geom_polygon geom_quantile

geom_raster geom_rect

geom_ribbon geom_rug

geom_segment geom_smooth

geom_step geom_text

geom_tile geom_violin

geom_vline

R包——ggplot2(一)的更多相关文章

  1. R包——ggplot2(二)

    关于ggplot包(二) 关于ggplot包(二) 标尺(Scale) 从前面可以看到,画图其实就是在做映射,不管是映射到不同的几何对象上,还是映射各种图形属性.在对图形属性进行映射之后,使用标尺可以 ...

  2. R语言 ggplot2包

    R语言  ggplot2包的学习   分析数据要做的第一件事情,就是观察它.对于每个变量,哪些值是最常见的?值域是大是小?是否有异常观测? ggplot2图形之基本语法: ggplot2的核心理念是将 ...

  3. R 包

    [下面列出每个步骤最有用的一些R包] .数据导入 以下R包主要用于数据导入和保存数据: feather:一种快速,轻量级的文件格式:在R和python上都可使用 readr:实现表格数据的快速导入 r ...

  4. 极简 R 包建立方法--转载

    https://cosx.org/2013/11/building-r-packages-easily/ 最近想试一下捣腾一个 R 包出来,故参考了一些教程.现在看到的最好的就是谢益辉大大之前写过的开 ...

  5. Ubuntu安装R及R包

    安装R $sudo apt-get update $sudo apt-get install r-base $sudo apt-get install r-base-dev 安装一些可能的依赖包 $s ...

  6. R语言ggplot2 简介

    ggplot2是一个绘制可视化图形的R包,汲取了R语言基础绘图系统(graphics) 和l attice包的优点,摒弃了相关的缺点,创造出来的一套独立的绘图系统: ggplot2 有以下几个特点: ...

  7. R包开发过程记录

    目的 走一遍R包开发过程,并发布到Github上使用. 步骤 1. 创建R包框架 Rsutdio --> File--> New Project--> New Directory - ...

  8. 普通用户安装 R 包

    转自 http://bnuzhutao.cn/archives/901 一般 R 语言的书籍上,介绍安装 R 包的方法都是这样的: install.packages("packagename ...

  9. R包介绍

    R语言的使用,很大程度上是借助各种各样的R包的辅助,从某种程度上讲,R包就是针对于R的插件,不同的插件满足不同的需求,截至2013年3月6日,CRAN已经收录了各类包4338个. 一. R语言包的安装 ...

随机推荐

  1. ruby中输入命令行编译sass(ruby小白)

    Ruby(或cmd中)输入命令行编译sass步骤如下: (1)举例而言:首先在F盘下建立一个总文件夹,比如test文件夹:其次在该文件夹下建立html,images,js,sass等文件夹. (2)在 ...

  2. HTML文本框

    文本框样式大全   输入框景背景透明:<input style="background:transparent;border:1px solid #ffffff"> 鼠 ...

  3. left join 和 left outer join 的区别

    left join 和 left outer join 的区别 通俗的讲:    A   left   join   B   的连接的记录数与A表的记录数同    A   right   join   ...

  4. 文件上传input type="file"样式美化

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. HTML中的uniqueID

    Web页面上元素的name属性本身是可以重复的,理论上讲id是不可以重复的,但是现在的浏览器对重复的id都是默许的,可能有时候页面是需要一个唯一编号的.IE浏览器为页面上的所有元素都是提供了一个唯一名 ...

  6. mysql函数操作

    <?php try{ $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); }catc ...

  7. canvas1

    canvas学习(一) Canvas 学习之路 (一) canvas 是H5 里面神一样的东西,使得只是通过html和js就能做出非常棒的游戏和画面. 因为对前端无限的爱好,更加对canvas充满好奇 ...

  8. Top free and open source log management software

    As mentioned in the previous post, in my quest to find an alternative to Kiwi Syslog, I looked at a ...

  9. 如何評鑑一家SMT代工廠

    我們一般稱專業的「電子代工廠」為 EMS(Electronics Manufacturing Service,電子製造服務業) 或 CM(Contract Manufacturer,合同製造廠),這些 ...

  10. lua IDE for cocos2d-x development

    原文链接:http://hi.baidu.com/balduc0m/item/648093dad238bd2a39f6f78e lua IDE for cocos2d-x development -- ...