R语言绘图系列:
- R语言可视化及作图1--基础绘图(par函数,散点图,盒形图,条形图,直方图)
- R语言可视化及作图2--低级绘图函数
- R语言可视化及作图3--图形颜色选取
- R语言可视化及作图4--qplot和ggplot2美学函数
- R语言可视化及作图5--ggplot2基本要素和几何对象汇总
- R语言可视化及作图6--ggplot2之点图、条形图、盒形图、直方图、线图
- R语言可视化及作图7--ggplot2之标签、图例和标题绘制
- R语言可视化及作图8--坐标轴自定义和坐标系转换
一. 主题函数
1. ggtheme {ggplot2}
1.1 绘制一张题
library(ggplot2)
mg <- ggplot(mtcars,aes(x=mpg,y=wt))+geom_point()
mg #默认灰色背景
1.2 theme_bw函数
a <- mg+theme_bw()+geom_text(aes(x=30,y=5),label='theme_bw()',color=
'red',size=10)
a #白色网格背景
1.3 theme_classic函数
b <- mg+theme_classic()+geom_text(aes(x=25,y=5),label='theme_classic()',
color='red',size=10)
b #经典白色背景
1.4 theme_dark
c <- mg+theme_dark()+geom_text(aes(x=25,y=5),label='theme_dark()',
color='red',size=10)
c #暗色背景
1.5 theme_light函数
d <- mg+theme_light()+geom_text(aes(x=25,y=5),label='theme_light()',
color='red',size=10)
d
1.6 theme_get函数
e <- mg+theme_get()+geom_text(aes(x=25,y=5),label='theme_get()',
color='red',size=10)
e#与原始背景类似
1.7 theme_linedraw函数
f <- mg+theme_linedraw()+geom_text(aes(x=25,y=5),label='theme_linedraw()',
color='red',size=10)
f
1.8 theme_replace
g <- mg+theme_replace()+geom_text(aes(x=25,y=5),label='theme_replace()',
color='red',size=10)
g #与默认类似
1.9 theme_minimal
h <- mg+theme_minimal()+geom_text(aes(x=25,y=5),label='theme_minimal()',
color='red',size=10)
h #x轴y轴不见了,只有网格
1.10 theme_void
i <- mg+theme_void()+geom_text(aes(x=25,y=5),label='theme_void()',
color='red',size=10)
i #只有点
2. ggthemes包
绘制一张图
library(ggplot2)
library(ggthemes)
p <- ggplot(mtcars,aes(x=wt,y=mpg,color=factor(gear)))+
geom_point()+labs(title = 'Cars')+
theme(plot.title = element_text(hjust = 0.5,family = 'Times New Roman'))
p
2.1 theme_economist
a <- p+theme_economist()+scale_color_economist()+
geom_text(aes(x=4,y=30),label='theme_economics',color='deeppink')
a
2.2 theme_solarized
b <- p+theme_solarized()+scale_color_solarized('blue')+
geom_text(aes(x=4,y=30),label='theme_solarized',color='deeppink')
b
2.3 theme_dark
c <- p+theme_solarized(light = FALSE)+scale_color_solarized('red')+
geom_text(aes(x=4,y=30),label='theme_dark',color='deeppink')
c
2.4 theme_dark2
d <- p+theme_solarized_2(light = FALSE)+scale_color_solarized('blue')+
geom_text(aes(x=4,y=30),label='theme_dark2',color='deeppink')
d
2.5 theme_stata
e <- p+theme_stata()+scale_color_stata()+
geom_text(aes(x=4,y=30),label='theme_stata',color='deeppink')
e #模仿stata生成的图
2.6 theme_igray
f <- p+theme_igray()+geom_text(aes(x=4,y=30),label='theme_igray',color='deeppink')
f
2.7 theme_wsj
h <- p+theme_wsj()+scale_color_wsj('colors6','')+
geom_text(aes(x=4,y=30),label='theme_wsj',color='deeppink')
h
2.8 theme_calc
i <- p+theme_calc()+scale_color_calc()+
geom_text(aes(x=4,y=30),label='theme_calc',color='deeppink')
i
2.9 theme_pander
j <- p+theme_pander()+scale_color_pander()+
geom_text(aes(x=4,y=30),label='theme_pander',color='deeppink')
j
2.10 theme_hc
k <- p+theme_hc()+scale_color_hc()+
geom_text(aes(x=4,y=30),label='theme_hc',color='deeppink')
k