您的位置:首页 > 其它

慕课—R语言之数据可视化—学习笔记 3.6ggplot2绘图系统

2016-05-22 21:14 471 查看
慕课上的关于ggplot2的视频地址
http://www.imooc.com/video/11581
不过此文不打算以此为ggplot2的学习笔记记录下来。而是综合自己在哈佛看到的有关R语言的教程的一份总结。

ggplot2 

下图为ggplot2所绘制



优缺点

Advantages of ggplot2
consistent 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的各个绘制图进行展示。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: