慕课—R语言之数据可视化—学习笔记 3.6ggplot2绘图系统
2016-05-22 21:14
471 查看
慕课上的关于ggplot2的视频地址
http://www.imooc.com/video/11581
不过此文不打算以此为ggplot2的学习笔记记录下来。而是综合自己在哈佛看到的有关R语言的教程的一份总结。
![](http://tutorials.iq.harvard.edu/R/Rgraphics/images/Economist1.png)
consistent underlying
plot specification at a high level of abstraction
very flexible
theme system for polishing plot appearance
mature and complete graphics system
many users, active mailing list
That said, there are some things you cannot (or should not) do With ggplot2:
3-dimensional graphics (see the rgl package)
Graph-theory type graphs (nodes/edges layout; see the igraph package)
Interactive graphics (see the ggvis package)
到该网站https://www.lincolninst.edu/subcenters/land-values/land-prices-by-state.asp)下载美国各州的房屋土地价格。使用exel打开后将表格第一行删掉,并且将里面所有的价格(数字前面有$)设置为数值型,另存为.csv格式。
library(ggplot2)
house=read.csv("landdata-states-2015q3.csv")
head(house[1:5])
hist(house$HomeValue)#使用base绘图系统
ggplot(house,aes(x=HomeValue))+geom_histogram()//使用ggplot2系统
![](https://img-blog.csdn.net/20160522205248543?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
ggplot2 VS base绘制图像的复杂性
#使用base绘制
plot(HomeValue ~ Date,
data=subset(house, STATE == "MA"))
points(HomeValue ~ Date, col="red",
data=subset(house,STATE == "TX"))
legend(19750, 400000,
c("MA", "TX"), title="State",
col=c("black", "red"),
pch=c(1, 1))
![](https://img-blog.csdn.net/20160522210341750?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
#使用ggplot2绘制
ggplot(subset(house, STATE %in% c("MA", "TX")),
aes(x=Date,
y=HomeValue,
color=STATE))+
geom_point()
![](https://img-blog.csdn.net/20160522210412797?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
显然ggplot2绘制图像更胜一筹。
下面从美学角度对ggplot2的各个绘制图进行展示。
http://www.imooc.com/video/11581
不过此文不打算以此为ggplot2的学习笔记记录下来。而是综合自己在哈佛看到的有关R语言的教程的一份总结。
ggplot2
下图为ggplot2所绘制![](http://tutorials.iq.harvard.edu/R/Rgraphics/images/Economist1.png)
优缺点
Advantages of ggplot2consistent underlying
grammar of graphics(Wilkinson, 2005)
plot specification at a high level of abstraction
very flexible
theme system for polishing plot appearance
mature and complete graphics system
many users, active mailing list
That said, there are some things you cannot (or should not) do With ggplot2:
3-dimensional graphics (see the rgl package)
Graph-theory type graphs (nodes/edges layout; see the igraph package)
Interactive graphics (see the ggvis package)
案例
本案例来自哈佛R语音教程。案例我已经测试,需要自己修改。到该网站https://www.lincolninst.edu/subcenters/land-values/land-prices-by-state.asp)下载美国各州的房屋土地价格。使用exel打开后将表格第一行删掉,并且将里面所有的价格(数字前面有$)设置为数值型,另存为.csv格式。
library(ggplot2)
house=read.csv("landdata-states-2015q3.csv")
head(house[1:5])
hist(house$HomeValue)#使用base绘图系统
ggplot(house,aes(x=HomeValue))+geom_histogram()//使用ggplot2系统
ggplot2 VS base绘制图像的复杂性
#使用base绘制
plot(HomeValue ~ Date,
data=subset(house, STATE == "MA"))
points(HomeValue ~ Date, col="red",
data=subset(house,STATE == "TX"))
legend(19750, 400000,
c("MA", "TX"), title="State",
col=c("black", "red"),
pch=c(1, 1))
#使用ggplot2绘制
ggplot(subset(house, STATE %in% c("MA", "TX")),
aes(x=Date,
y=HomeValue,
color=STATE))+
geom_point()
显然ggplot2绘制图像更胜一筹。
下面从美学角度对ggplot2的各个绘制图进行展示。
相关文章推荐
- HDU 1847 Good Luck in CET-4 Everybody! (博弈)
- Spark函数讲解:checkpoint
- C++——第六次作业。
- laravel 5.1 源码阅读
- poj 1088 滑雪
- 深入理解Docker Volume(二)
- 命令模式
- 第12周项目1--实现复数类中的运算重载(3)
- javascript语法之函数案例练习
- javascript语法之函数案例练习
- 数据结构实验之栈三:后缀式求值
- 新一代与12-factors:构建,发布,运行
- Probability to form a triangle by splitting a stick
- 压力测试和性能测试的区别
- thrift开发的例子(一)---python为例
- C位域与大小端字节序 间关系
- Spark函数讲解:coalesce
- Memcached安装gzip: stdin: not in gzip format问题解决办法
- IM分布式压力测试工具设计(类loadrunner)
- scons是一个Python写的自动化构建工具,和GNU make相比优点明显