R包——ggplot2(一)
2024-08-31 04:13:37
关于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
最新文章
- WPF 自定义柱状图 BarChart
- 2D几何变换
- 3.1 ARM汇编编程概述
- Yosemite 升级后第三方SSD TRIM失败不能进入系统处理
- python第三方库系列之十九--python測试使用的mock库
- 一直想测试的NGINX变量输出,最于有办法了。
- jQuery模板插件jsrender
- 在SSH框架中增加SiteMesh的支持
- 一起学习c++11——c++11中的新增的容器
- wstring操作与普通段字符操作对照表
- [ipsec][crypto] 什么是AEAD加密算法中的AAD 及aad length
- hasura graphql-engine v1.0.0-alpha26 版本新功能
- php -- 检查是否存在
- PTA (Advanced Level) 1010 Radix
- IO阻塞模型 非阻塞模型
- HDU6315 Naive Operations(多校第二场1007)(线段树)
- HDU 5934 (强连同分量+缩点)
- 研华 RISC超低功耗3.5”单板电脑
- openfire插件开发环境
- selenium+python中,框架中,怎么返回上一个菜单