ggplot2绘图系统——主题函数

1. theme函数

​theme_*​​​系列函数提供了9种不同的风格。
​​​theme_grey/gray/bw/linedraw/light/minimal/classic/dark/void​​都只有2个参数:base_size表示文字大小,base_family表示字体。

mg <- ggplot(mtcars,aes(x=mpg,y=wt))+geom_point()
a=mg+theme_bw()+geom_text(aes(x=25,y=5),label='theme_bw',color='red',size=10)
b=mg+theme_classic()+geom_text(aes(x=25,y=5),label='theme_classic',color='red',size=10)
c=mg+theme_dark()+geom_text(aes(x=25,y=5),label='theme_dark',color='red',size=10)
d=mg+theme_light()+geom_text(aes(x=25,y=5),label='theme_light',color='red',size=10)
e=mg+theme_get()+geom_text(aes(x=25,y=5),label='theme_get',color='red',size=10)
f=mg+theme_linedraw()+geom_text(aes(x=25,y=5),label='theme_linedraw',color='red',size=10)
g=mg+theme_replace()+geom_text(aes(x=25,y=5),label='theme_replace',color='red',size=10)
h=mg+theme_minimal()+geom_text(aes(x=25,y=5),label='theme_minimal',color='red',size=10)
i=mg+theme_void()+geom_text(aes(x=25,y=5),label='theme_void',color='red',size=10)

grid.arrange(a,b,c,d,e,f,g,h,i,ncol=3)

R语言与医学统计图形-【26】ggplot2主题函数_自定义

2. ggthemes包

ggplot2扩展包,包括主题函数和标度函数。
ggthemes包种最常见的12种主题。

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'))
a <- p+theme_economist()+scale_color_economist()+
  geom_text(aes(x=4,y=30),label='theme_economist',color='deeppink')
b <- p+theme_solarized()+scale_color_solarized('blue')+
  geom_text(aes(x=4,y=30),label='theme_solarized',color='deeppink')
c <- p+theme_solarized(light = FALSE)+scale_color_solarized('red')+
  geom_text(aes(x=4,y=30),label='theme_dark',color='deeppink')
d <- p+theme_solarized(light = FALSE)+scale_color_solarized('blue')+
  geom_text(aes(x=4,y=30),label='theme_dark2',color='deeppink')
grid.arrange(a,b,c,d,ncol=2)

R语言与医学统计图形-【26】ggplot2主题函数_微信公众号_02

e <- p+theme_stata()+scale_color_stata()+geom_text(aes(x=4,y=30),label='theme_stata',color='deeppink')
f <- p+theme_igray()+geom_text(aes(x=4,y=30),label='theme_igray',color='deeppink')
g <- p+theme_igray()+scale_color_tableau()+geom_text(aes(x=4,y=30),label='theme_igray',color='deeppink')
h <- p+theme_wsj()+scale_color_wsj('colors6','')+geom_text(aes(x=4,y=30),label='theme_wsj',color='deeppink')
grid.arrange(e,f,g,h,ncol=2)

R语言与医学统计图形-【26】ggplot2主题函数_微信公众号_03

i <- p+theme_calc()+scale_color_calc()+geom_text(aes(x=4,y=30),label='theme_calc',color='deeppink')
j <- p+theme_pander()+scale_color_pander()+geom_text(aes(x=4,y=30),label='theme_pander',color='deeppink')
k <- p+theme_hc()+scale_color_hc()+geom_text(aes(x=4,y=30),label='theme_hc',color='deeppink')
l <- p+theme_hc(bgcolor = 'darkunica')+scale_color_hc('darkunica')+geom_text(aes(x=4,y=30),label='theme_hc2',color='deeppink')
grid.arrange(i,j,k,l,ncol=2)

R语言与医学统计图形-【26】ggplot2主题函数_微信公众号_04

除了ggthemes包,还有artyfarty和ggthemr包也可设置主题,或者自定义主题函数。


作者:Bioinfarmer

 若要及时了解动态信息,请关注同名微信公众号:Bioinfarmer。