1 画出《ggplot2 Elegant Graphics for Data Analysis》书中第102页图6.5的三幅图
以下2-3任选一题
2 用极坐标系描画钻石数据集中的若干变量,能集成的信息越多越好 3 抓取Oracle,Apple,MS三家股票从2006年到目前的数据,用动态气泡图的方法,显示它们的股价,成交量等的变化趋势
书中的代码略有瑕疵,需修改。
library(ggplot2)
library(scales)
library(zoo)
require(gridExtra)
#区域设置(选项"C"表示关闭区域特定编码方式,见help):
lct <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
## [1] "C"
#在plot2中,将major改为breaks,加上labels=date_format("%Y")选项
#在plot3中,将format选项移到as.Date的括号里面或者删掉
plot <- qplot(date, psavert, data = economics, geom = "line") +
ylab("Personal savings rate") +
geom_hline(xintercept = 0, colour = "grey50") +
scale_x_date(breaks=as.Date(yearmon(seq(1967,2007,by=5))), labels=date_format("%Y"))
plot2 <- plot + scale_x_date(breaks=as.Date(yearmon(seq(1967,2007,by=10))), labels=date_format("%Y"))
plot3 <- plot + scale_x_date(
limits = as.Date(c("2004-01-01", "2005-01-01"),format = "%Y-%m-%d"), labels=date_format("%Y-%m-%d")
)
#将三张图画在一个画板中
grid.arrange(plot,plot2,plot3,ncol=3)
#还原区域设置
Sys.setlocale("LC_TIME", lct)
## [1] "Chinese (Simplified)_People's Republic of China.936"
#参考:http://f.dataguru.cn/thread-221819-1-1.html
# 看不同纯度的数量
p <- ggplot(diamonds, aes(x=clarity,fill=clarity))
p + geom_bar(width = 1) +
coord_polar() +
ggtitle("Distribution of diamonds' clarity") +
theme(plot.title = element_text(size = 20,face="bold"))
# 看不同切割质量的数量
p <- ggplot(diamonds, aes(x=cut,fill=cut))
p + geom_bar(width = 1) +
coord_polar() +
ggtitle("Distribution of diamonds' cut") +
theme(plot.title = element_text(size = 20,face="bold"))
# 不同纯度下的切割质量分布
p <- ggplot(diamonds, aes(x=clarity, fill=cut))
p + geom_bar(position="fill") +
coord_polar(theta = "y") +
ggtitle("Distribution of diamonds' cut under clarity") +
theme(plot.title = element_text(size = 20,face="bold"))