element_text()绘制标签和标题(colour,size,lineheight,hjust,angle ,face ,vjust,family)
basic +
theme_set(theme_grey()) +#要不要theme_set()都可
theme(plot.title = element_text(size = 20,color = "pink"))

basic + theme(plot.title = element_text(size = 20,color = "pink",hjust = 1)) +
theme_grey()#<<默认的浅灰色背景和白色网格线,但是其他的效果都没了,这个设置得放在前面

basic + theme(plot.title = element_text(size = 20,color = "pink",face = "bold",angle = 3))

element_rect()绘制背景使用的矩形(colour,linetype,size,fill)
#plot.background整个图形背景
basic + theme(plot.background = element_rect(fill = "#3366FF"))

basic + theme(plot.background = element_rect(colour = "red",linetype = "dotted",size = 5))

#panel.background面板背景
basic + theme(panel.background = element_rect(colour = "red",linetype = "dotted",size = 5,fill = "yellow"))

#保存前一个生成的图片
ggsave(file = "outputtt.png",width = 6,height = 6,dpi = 300)
四种方式生成三图在一起(要一块一起运行,一句一句来不行,下面三个都是)提倡第四种
############
library(grid)
library(ggplot2)
# prepare ggplot charts
p.hist.len <- ggplot(iris) + geom_histogram(aes(x=Sepal.Length))
p.hist.wid <- ggplot(iris) + geom_histogram(aes(x=Sepal.Width)) + coord_flip()
p.scatter <- ggplot(iris) + geom_point(aes(x=Sepal.Length, y=Sepal.Width))
# create viewports
grid.newpage()
vp.len <- viewport(x=0, y=0.66, width=0.66, height=0.34, just=c("left", "bottom"))
vp.wid <- viewport(x=0.66, y=0, width=0.34, height=0.66, just=c("left", "bottom"))
vp.scatter <- viewport(x=0, y=0, width=0.66, height=0.66, just=c("left", "bottom"))
# direct the charts into the specified viewport
print(p.hist.len, vp=vp.len)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
print(p.hist.wid, vp=vp.wid)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
print(p.scatter, vp=vp.scatter)

##################
##################
# top left panel
grid.newpage()
vp.len <- viewport(x=0, y=0.66, width=0.66, height=0.34, just=c("left", "bottom"))
pushViewport(vp.len)
print(p.hist.len, newpage=F)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
upViewport() # 返回父节点
# bottom right panel
vp.wid <- viewport(x=0.66, y=0, width=0.34, height=0.66, just=c("left", "bottom"))
pushViewport(vp.wid)
print(p.hist.wid, newpage=F)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
upViewport()
# bottom left panel
vp.scatter <- viewport(x=0, y=0, width=0.66, height=0.66, just=c("left", "bottom"))
pushViewport(vp.scatter)
print(p.scatter, newpage=F)
upViewport()

grid.newpage()
pushViewport(viewport(layout = grid.layout(3, 3)))
print(p.scatter, vp=viewport(layout.pos.row=2:3, layout.pos.col=1:2))
print(p.hist.len, vp=viewport(layout.pos.row=1, layout.pos.col=1:2))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
print(p.hist.wid, vp=viewport(layout.pos.row=2:3, layout.pos.col=3))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

a <- basic + theme(panel.background = element_rect(fill = "red"))
b <- basic + theme(panel.background = element_rect(fill = "yellow"))
d <- basic + theme(panel.background = element_rect(fill = "blue"))
grid.newpage()
pushViewport(viewport(layout = grid.layout(2,2)))#产生一个2*2的地盘
vplayout <- function(x,y){
viewport(layout.pos.row = x, layout.pos.col = y)
}
print(a,vp = vplayout(1,1:2))
print(b,vp = vplayout(2,1))
print(d,vp = vplayout(2,2))

来个变式练习,(我可真是太厉害了呢)
aa <- qplot(mpg, hp, data = mtcars,geom = "line")
bb <- qplot(wt , hp, data = mtcars)+geom_smooth(se = F)
dd <- qplot(qsec, hp, data = mtcars,geom = "path")
grid.newpage()
pushViewport(viewport(layout = grid.layout(1,3)))#产生一个2*2的地盘
vplayout <- function(x,y){
viewport(layout.pos.row = x, layout.pos.col = y)
}
print(aa,vp = vplayout(1,1))
print(bb,vp = vplayout(1,2))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
print(dd,vp = vplayout(1,3))
