凡是和数据无关的图形设置内容理论上都可以归为主题类,但考虑到一些内容(如坐标轴)的特殊性,可以允许例外的情况。主题的设置相当繁琐,很容易就占用了 大量的作图时间,应尽量把这些东西简化,把注意力主要放在数据分析上。基于这种考虑,ggplot2主题设置的内容虽然相当多,本文仅在总体上作一简单介 绍。

1 theme函数及其参数

让使用者在数据分析阶段能专注于数据而不是图形细节,这是数据可视化分析工具是否合格的标准之一。某些作图软件(或自以为有作图能力的软件)给出的初始图 形简直惨不忍睹,不花时间修改字体、边距、底纹这些东西就恶心得没法继续分析数据。ggplot2做得还可以:即使不做任何设置,多数情况下作出的图形都 还不错,不丑陋也不妖艳,不会分散用户的注意力。这得益于ggplot2的几个预设主题。

ggplot2的四个预设主题我们在前面已经预览过了,下面我们看看主题都包含了哪些东西。这很容易,把ggplot2默认主题的设置函数theme_gray()的代码拿出来看看就知道了:

  1. library(ggplot2) theme_gray # 函数名不加括号,获得函数的代码
  1. ## function (base_size = 12, base_family = "") 
    ## { 
    ## theme(line = element_line(colour = "black", size = 0.5, linetype = 1, 
    ## lineend = "butt"), rect = element_rect(fill = "white", 
    ## colour = "black", size = 0.5, linetype = 1), text = element_text(family = base_family, 
    ## face = "plain", colour = "black", size = base_size, hjust = 0.5, 
    ## vjust = 0.5, angle = 0, lineheight = 0.9), axis.text = element_text(size = rel(0.8),
     ## colour = "grey50"), strip.text = element_text(size = rel(0.8)), 
    ## axis.line = element_blank(), axis.text.x = element_text(vjust = 1),
     ## axis.text.y = element_text(hjust = 1), axis.ticks = element_line(colour = "grey50"), 
    ## axis.title.x = element_text(), axis.title.y = element_text(angle = 90), 
    ## axis.ticks.length = unit(0.15, "cm"), axis.ticks.margin = unit(0.1, 
    ## "cm"), legend.background = element_rect(colour = NA), 
    ## legend.margin = unit(0.2, "cm"), legend.key = element_rect(fill = "grey95", 
    ## colour = "white"), legend.key.size = unit(1.2, "lines"), 
    ## legend.key.height = NULL, legend.key.width = NULL, legend.text = element_text(size = rel(0.8)),
     ## legend.text.align = NULL, legend.title = element_text(size = rel(0.8), 
    ## face = "bold", hjust = 0), legend.title.align = NULL, 
    ## legend.position = "right", legend.direction = NULL, legend.justification = "center", 
    ## legend.box = NULL, panel.background = element_rect(fill = "grey90", 
    ## colour = NA), panel.border = element_blank(), panel.grid.major = element_line(colour = "white"), 
    ## panel.grid.minor = element_line(colour = "grey95", size = 0.25),
     ## panel.margin = unit(0.25, "lines"), strip.background = element_rect(fill = "grey80",
     ## colour = NA), strip.text.x = element_text(), strip.text.y = element_text(angle = -90), 
    ## plot.background = element_rect(colour = "white"), plot.title = element_text(size = rel(1.2)), 
    ## plot.margin = unit(c(1, 1, 0.5, 0.5), "lines"), complete = TRUE) ## }
     ##

看穿了吧,没神秘感了。它无非是一个具有两个参数的函数:base_size和base_family。其主题部分直接应用了另外一个函数:theme。它就是ggplot2的主题设置函数。这个theme函数的产生看起来非常简单:

  1. # 函数说明,非运行代码 theme(..., complete = FALSE)

但dotdotdot(···)参数却内涵丰富,它可以设置很多内容。

参数 设置内容 继承自
line 所有线属性  
rect 所有矩形区域属性  
text 所有文本相关属性  
title 所有标题属性  
axis.title 坐标轴标题 text
axis.title.x x轴属性 axis.title
axis.title.y y轴属性 axis.title
axis.text 坐标轴刻度标签属性 text
axis.text.x 属性和继承和前面类似,不再重复  
axis.text.y    
axis.ticks 坐标轴刻度线 line
axis.ticks.x    
axis.ticks.y    
axis.ticks.length 刻度线长度  
axis.ticks.margin 刻度线和刻度标签之间的间距  
axis.line 坐标轴线 line
axis.line.x    
axis.line.y    
legend.background 图例背景 rect
legend.margin 图例边界  
legend.key 图例符号  
legend.key.size 图例符号大小  
legend.key.height 图例符号高度  
legend.key.width 图例符号宽度  
legend.text 图例文字标签  
legend.text.align 图例文字标签对齐方式 0为左齐,1为右齐
legend.title 图例标题 text
legend.title.align 图例标题对齐方式  
legend.position 图例位置 left, right, bottom, top, 两数字向量
legend.direction 图例排列方向 "horizontal" or "vertical"
legend.justification 居中方式 center或两数字向量
legend.box 多图例的排列方式 "horizontal" or "vertical"
legend.box.just 多图例居中方式  
panel.background 绘图区背景 rect
panel.border 绘图区边框 rect
panel.margin 分面绘图区之间的边距  
panel.grid 绘图区网格线 line
panel.grid.major 主网格线  
panel.grid.minor 次网格线  
panel.grid.major.x    
panel.grid.major.y    
panel.grid.minor.x    
panel.grid.minor.y    
plot.background 整个图形的背景  
plot.title 图形标题  
plot.margin 图形边距 top, right, bottom, left
strip.background 分面标签背景 rect
strip.text 分面标签文本 text
strip.text.x    
strip.text.y    

除一些尺寸设置有关的内容外(需要用grid包的unit函数设置),几乎所有元素都在theme函数内使用 element_line,element_rect,element_text和element_blank函数设置,使用方法参考这几个函数的参数说 明即可,这里不再一一举例说明。

text, line, rect和title是最顶层的元素,理论上可以做全局设定,但当前版本ggplot2还没有实现,可以根据情况做一些调整:

  1. <- LETTERS[1:10] y <- abs(rnorm(10)) (<- qplot(= x, y = y, color = x, fill = x,geom = c("line", "point"), group = 1) + labs(title = "The figure title.", xlab ="Factor", ylab = "Value") + theme(text = element_text(color = "red", size = 16), line =element_line(color = "blue"), rect = element_rect(fill = "white"))) p +theme(panel.background = element_rect(fill = "transparent", color = "gray"), legend.key= element_rect(fill = "transparent", color = "transparent"), axis.text =element_text(color = "red"))

全局text和rect设置对部分元素有作用,line基本不起作用。

2 自定义主题

图形细节设置虽然繁琐,但是在R中可以相当简单。由于自己使用的或者杂志要求的图形外观一般都很固定,我们可以使用ggplot2的theme函数非常方便地定义自己的图形主题。下面是我自用的一个主题函数,主要作的改动是坐标轴刻度朝向和统一了图形各个区域的背景颜色:

  1. ##' A nice-looking ggplot2 theme: inward axis ticks, legend title excluded, and uniform background. ##' @title A nice-looking ggplot2 theme ##' @param ... ##' Parameters passed to theme_classic() function. ##' @param bg ##' Color string (default 'white') for user defined uniform background. ##' @return ##' ggplot2 theme object. ##' @examples ##' library(ggplot2) ##' qplot(x=carat, y=price, color=cut, data=diamonds) + theme_zg() ##' @author ZGUANG ##' @export theme_zg <- function(..., bg='white'){ require(grid)theme_classic(...) + theme(rect=element_rect(fill=bg), plot.margin=unit(rep(0.5,4),'lines'), panel.background=element_rect(fill='transparent', color='black'),panel.border=element_rect(fill='transparent', color='transparent'),panel.grid=element_blank(), axis.title = element_text(color='black', vjust=0.1),axis.ticks.length = unit(-0.4,"lines"), axis.ticks = element_line(color='black'),axis.ticks.margin = unit(0.8,"lines"), legend.title=element_blank(),legend.key=element_rect(fill='transparent', color='transparent')) }

自定义的主题可以编入自己的R语言包中,方便调用。如果觉得你的主题很有代表性,可以发给ggplot2的作者H.W.,让他加到ggplot2的下一个发行版中。比如上面上面函数加入ggplot2后就可以直接调用:

  1. <- qplot(= x, y = y, color = x, fill = x, geom = c("line", "point"), group = 1) +labs(title = "The figure title.", xlab = "Factor", ylab = "Value") p + theme_zg() p +theme_zg(base_size = 16, bg = "gray90")

3 结束语

本系列博文到此结束。ggplot2还在完善过程中,一些新功能可能不断会实现,感谢H.W的努力。有一些本应属于主题类的东西不能在theme_set 函数中设置,需要使用特殊的函数,如坐标轴翻转、刻度设置等,在这不再介绍。如果想比较透彻的学习,建议把ggplot2项目从GitHub克隆下来研究 它的代码。更好的方法是研究ggmap和ggbio这两个包,它们的代码本身就是ggplot2应用的最好范例。尤其是ggmap,H.W.是它的作者之 一。

本系列文章的例子基于ggplot2 0.9.3.1版本,如果发现一些代码运行不正确或效果图有变化,应该是版本不同造成。文章后都有sessionInfo信息可参考。

  1. sessionInfo()
  1. ## R version 3.0.1 (2013-05-16) 
    ## Platform: x86_64-pc-linux-gnu (64-bit) 
    ## ## locale: 
    ## [1] LC_CTYPE=zh_CN.UTF-8 LC_NUMERIC=C
     ## [3] LC_TIME=zh_CN.UTF-8 LC_COLLATE=zh_CN.UTF-8 
    ## [5] LC_MONETARY=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8
     ## [7] LC_PAPER=C LC_NAME=C 
    ## [9] LC_ADDRESS=C LC_TELEPHONE=C #
    # [11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C 
    ## ## attached base packages: 
    ## [1] grid tcltk stats graphics grDevices utils datasets 
    ## [8] methods base ## ## other attached packages: 
    ## [1] zblog_0.0.1 knitr_1.3.5 ggplot2_0.9.3.1 
    ## ## loaded via a namespace (and not attached): 
    ## [1] colorspace_1.2-2 dichromat_2.0-0 digest_0.6.3 
    ## [4] evaluate_0.4.4 formatR_0.8 gtable_0.1.2 
    ## [7] highr_0.1.2 labeling_0.2 MASS_7.3-27 
    ## [10] munsell_0.4.2 plyr_1.8 proto_0.3-10 
    ## [13] RColorBrewer_1.0-5 reshape2_1.2.2 scales_0.2.3 
    ## [16] stringr_0.6.2 tools_3.0.1

ggplot2作图详解7(完):主题(theme)设置的更多相关文章

  1. ggplot2作图详解:入门函数qplot

    ggplot2作图详解:入门函数qplot   ggplot2的功能不用我们做广告,因为它的作者Hadley Wickham就说ggplot2是一个强大的作图工具,它可以让你不受现有图形类型的限制,创 ...

  2. HTTPD之二————HTTPD服务详解————httpd的配置文件常见设置

    HTTPD之二----HTTPD服务详解----httpd的配置文件常见设置 HTTP服务器应用 http服务器程序 httpd apache nginx lighttpd 应用程序服务器 IIS,a ...

  3. 【甘道夫】HBase基本数据操作详解【完整版,绝对精品】

    引言 之前详细写了一篇HBase过滤器的文章,今天把基础的表和数据相关操作补上. 本文档参考最新(截止2014年7月16日)的官方Ref Guide.Developer API编写. 所有代码均基于“ ...

  4. HBase基本数据操作详解【完整版,绝对精品】

    欢迎转载,请注明来源: http://blog.csdn.net/u010967382/article/details/37878701 概述 对于建表,和RDBMS类似,HBase也有namespa ...

  5. 【转载】HBase基本数据操作详解【完整版,绝对精品】

    转载自: http://blog.csdn.net/u010967382/article/details/37878701 概述 对于建表,和RDBMS类似,HBase也有namespace的概念,可 ...

  6. fish_redux使用详解---看完就会用!

    说句心里话,这篇文章,来来回回修改了很多次,如果认真看完这篇文章,还不会写fish_redux,请在评论里喷我. 前言 来学学难搞的fish_redux框架吧,这个框架,官方的文档真是一言难尽,比fl ...

  7. Python虚拟环境和包管理工具Pipenv的使用详解--看完这一篇就够了

    前言 Python虚拟环境是一个虚拟化,从电脑独立开辟出来的环境.在这个虚拟环境中,我们可以pip安装各个项目不同的依赖包,从全局中隔离出来,利于管理. 传统的Python虚拟环境有virtualen ...

  8. smb.conf详解[未完]

    看着玩意看的吐血!!!! baidu\google充斥着一堆错误的文章及翻译,samba.org上动辄就是this document is old and might be incurrent. 不过 ...

  9. Linux tomcat安装详解(未完)

    转: http://blog.csdn.net/lcyaiym/article/details/76696192

随机推荐

  1. c# SQL Server数据库操作-数据适配器类:SqlDataAdapter

    SqlDataAdapter类主要在MSSQL与DataSet之间执行数据传输工具,本节将介绍如何使用SqlDataAdapter类来填充DataSet和MSSQL执行新增.修改..删除等操作. 功能 ...

  2. UNIX的插头问题

    UNIX的插头问题 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 你负责为联合国互联网执行组织(UNIX)的周年会议布置会议室. ...

  3. Visual Studio实用小技巧

    有一个有关微软Office的笑话,说的是它的特性太多: 当你觉得自己发现了一个Office的新特性时,它已经存在很多年了. 本文将介绍一些在Visual Studio(免费下载)中很实用却被忽略的小技 ...

  4. ebay商品基本属性组合成数据表格式,可用上传到系统递交数据

    该刊登表设计是利用VB写的,当时因为两个系统的数据不能直接对接,又copy并且组合SKU,一个表格一个表格填写,比较麻烦,还好刊登系统可以允许用excel表格上传数据 所以就下好模板,学了VB语言,在 ...

  5. 导出无法正常启动的VMware虚拟机中的文件

    为了使用网银,在MacBook中用VMware虚拟机跑Windows 8.今天在使用Windows 8时,Windows Update自动安装了更新并自动重启,结果怎么也启动不起来了.不是停在&quo ...

  6. square-and-multiply algorithm

    https://en.wikipedia.org/wiki/Square-and-multiply_algorithm

  7. ETL__pentaho__SPOON_PDI

    Pentaho Data Integration (PDI, also called Kettle),是pentaho的etl工具.虽然etl工具一般都用在数据仓库环境中,可是,PDI还是可以做以下事 ...

  8. Django - ORM - 进阶

    一.多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是 ...

  9. 007-spring cache-缓存实现-02-springboot ehcahe3实现、springboot caffeine实现

    一.springboot ehcahe3实现步骤 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvider.Ehcache是一种广泛 ...

  10. C# 反双曲余弦函数

    反双曲余弦函数的定义是: T1 = Math.Log(t + Math.Sqrt(t * t - 1)); 1. 叉乘(cross product),也叫向量的外积.向量积.顾名思义,求下来的结果是一 ...