library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------------------------------------ tidyverse 1.2.1 --
## √ ggplot2 3.2.1     √ purrr   0.3.2
## √ tibble  2.1.2     √ dplyr   0.8.3
## √ tidyr   1.0.0     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.1
## Warning: package 'tidyr' was built under R version 3.6.1
## Warning: package 'dplyr' was built under R version 3.6.1
## -- Conflicts --------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)
child2016<- read_csv("./child2016change.csv")
## Parsed with column specification:
## cols(
##   age = col_double(),
##   gender = col_double(),
##   urban = col_double(),
##   edu_expect = col_double(),
##   parents_edu = col_double(),
##   finc = col_double(),
##   care_edu = col_double(),
##   communicate = col_double(),
##   total_care = col_double(),
##   tutor_fees_log = col_double(),
##   school_fees_log = col_double(),
##   other_log = col_double()
## )

关于主题

basic <- ggplot(child2016,aes(x = school_fees_log,y = other_log ,alpha = age)) +
  geom_point(color = "red") +
  theme_set(theme_bw()) +
  labs(title = "HAPPY ")
basic

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_line()绘制线条图中或线段(colour,size,linetype)

#主网格线
basic + theme(panel.grid.major = element_line(colour = "blue",size = 0.7,linetype = "dotted"))

basic + theme(panel.grid.major = element_line(colour = "blue",size = 1,linetype = "dashed"))

#直线和坐标轴
basic + theme(axis.line  = element_line(colour = "red",size = 3,linetype = "dashed"))

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))

一个图上面再叠一个小图

basic + theme(panel.grid.major = element_line(colour = "blue",size = 1,linetype = "dashed"))#<<这里先生成一个打底
a <- qplot(mpg, hp, data = mtcars,geom = "line")
subvp <- viewport(width = 0.4,height = 0.4,x =0.75 , y =0.2)
print(a,vp = subvp)

knitr::include_graphics("./me/img/ggplot.jpg")