技术标签: 可视化系列
转载自:http://blog.csdn.net/u014801157/article/details/24372531
凡是和数据无关的图形设置内容理论上都可以归 主题类 但考虑到一些内容(如 坐标轴 )的特殊性,可以允许例外的情况。主题的设置相当繁琐,很容易就占用了大量的作图时间,应尽量把这些东西简化,把注意力主要放在数据分析上。基于这种考虑,ggplot2主题设置的内容虽然相当多,本文仅在总体上作一简单介绍。
让使用者在数据分析阶段能专注于数据而不是图形细节,这是数据可视化分析工具是否合格的标准之一。某些软件作出的初始图形简直惨不忍睹,不花时间修改字体、边距、底纹这些东西就恶心得没法继续分析数据。ggplot2做得还可以:即使不做任何设置,多数情况下作出的图形都还不错,不丑陋也不妖艳,不会分散用户的注意力。这得益于ggplot2的几个预设主题。
ggplot2的四个预设主题我们在前面已经预览过了,下面我们看看主题都包含了哪些东西。这很容易,把ggplot2默认主题的设置函数theme_bw()的代码拿出来看看就知道了:
<span style="color: rgb(0, 42, 84);">library</span>(ggplot2) theme_gray <span style="color: rgb(0, 0, 195);"># 函数名不加括号,获得函数的代码</span>
## 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函数的产生看起来非常简单:
<span style="color: rgb(0, 0, 195);"># 函数说明,非运行代码</span> <span style="color: rgb(0, 42, 84);">theme</span>(..., <span style="color: rgb(0, 84, 0);">complete</span> = <span style="color: rgb(184, 93, 0);">FALSE</span>)
但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 |
所有元素都在theme函数内使用element_line,element_rect,element_text和element_blank函数设置,使用方法参考这几个函数的参数说明即可,这里不再一一举例说明。
text, line, rect和title是最顶层的元素,理论上可以做全局设定,但当前版本ggplot2还没有实现,可以根据情况做一些调整:
x <span style="color: rgb(84, 0, 84);"><strong><-</strong></span> LETTERS[<span style="color: rgb(184, 93, 0);">1</span>:<span style="color: rgb(184, 93, 0);">10</span>]; y <span style="color: rgb(84, 0, 84);"><strong><-</strong></span> <span style="color: rgb(0, 42, 84);">abs</span>(<span style="color: rgb(0, 42, 84);">rnorm</span>(<span style="color: rgb(184, 93, 0);">10</span>)) (p <span style="color: rgb(84, 0, 84);"><strong><-</strong></span> <span style="color: rgb(0, 42, 84);">qplot</span>(<span style="color: rgb(0, 84, 0);">x</span>=x, <span style="color: rgb(0, 84, 0);">y</span>=y, <span style="color: rgb(0, 84, 0);">color</span>=x, <span style="color: rgb(0, 84, 0);">fill</span>=x, <span style="color: rgb(0, 84, 0);">geom</span>=<span style="color: rgb(0, 42, 84);">c</span>(<span style="color: rgb(209, 0, 0);">'line'</span>,<span style="color: rgb(209, 0, 0);">'point'</span>), <span style="color: rgb(0, 84, 0);">group</span>=<span style="color: rgb(184, 93, 0);">1</span>) + <span style="color: rgb(0, 42, 84);">labs</span>(<span style="color: rgb(0, 84, 0);">title</span>=<span style="color: rgb(209, 0, 0);">'The figure title.'</span>, <span style="color: rgb(0, 84, 0);">xlab</span>=<span style="color: rgb(209, 0, 0);">'Factor'</span>, <span style="color: rgb(0, 84, 0);">ylab</span>=<span style="color: rgb(209, 0, 0);">'Value'</span>) + <span style="color: rgb(0, 42, 84);">theme</span>(<span style="color: rgb(0, 84, 0);">text</span>=<span style="color: rgb(0, 42, 84);">element_text</span>(<span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'red'</span>, <span style="color: rgb(0, 84, 0);">size</span>=<span style="color: rgb(184, 93, 0);">16</span>), <span style="color: rgb(0, 84, 0);">line</span>=<span style="color: rgb(0, 42, 84);">element_line</span>(<span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'blue'</span>), <span style="color: rgb(0, 84, 0);">rect</span>=<span style="color: rgb(0, 42, 84);">element_rect</span>(<span style="color: rgb(0, 84, 0);">fill</span>=<span style="color: rgb(209, 0, 0);">'white'</span>))) p + <span style="color: rgb(0, 42, 84);">theme</span>(<span style="color: rgb(0, 84, 0);">panel.background</span>=<span style="color: rgb(0, 42, 84);">element_rect</span>(<span style="color: rgb(0, 84, 0);">fill</span>=<span style="color: rgb(209, 0, 0);">'transparent'</span>, <span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'gray'</span>), <span style="color: rgb(0, 84, 0);">legend.key</span>=<span style="color: rgb(0, 42, 84);">element_rect</span>(<span style="color: rgb(0, 84, 0);">fill</span>=<span style="color: rgb(209, 0, 0);">'transparent'</span>, <span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'transparent'</span>), <span style="color: rgb(0, 84, 0);">axis.text</span>=<span style="color: rgb(0, 42, 84);">element_text</span>(<span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'red'</span>))
全局text和rect设置对部分元素有作用,line基本不起作用。
图形细节设置虽然繁琐,但是在R和ggplot2中可以相当简单。自己使用的或者杂志要求的图形外观一般都很固定,我们可以使用theme函数非常方便定义自己的图形主题:
<span style="color: rgb(0, 0, 195);">##' A nice-looking ggplot2 theme: inward axis ticks, legend title excluded, and uniform background.</span> <span style="color: rgb(0, 0, 195);">##' @title A nice-looking ggplot2 theme</span> <span style="color: rgb(0, 0, 195);">##' @param ...</span> <span style="color: rgb(0, 0, 195);">##' Parameters passed to theme_classic() function.</span> <span style="color: rgb(0, 0, 195);">##' @param bg</span> <span style="color: rgb(0, 0, 195);">##' Color string (default 'white') for user defined uniform background.</span> <span style="color: rgb(0, 0, 195);">##' @return</span> <span style="color: rgb(0, 0, 195);">##' ggplot2 theme object.</span> <span style="color: rgb(0, 0, 195);">##' @example</span> <span style="color: rgb(0, 0, 195);">##' library(ggplot2)</span> <span style="color: rgb(0, 0, 195);">##' qplot(x=carat, y=price, color=cut, data=diamonds) + theme_zg()</span> <span style="color: rgb(0, 0, 195);">##' @author ZG Zhao</span> <span style="color: rgb(0, 0, 195);">##' @export</span> theme_zg <span style="color: rgb(84, 0, 84);"><strong><-</strong></span> <span style="color: rgb(0, 0, 127);">function</span>(<span style="color: rgb(0, 84, 0);">...</span>, <span style="color: rgb(0, 84, 0);">bg</span>=<span style="color: rgb(209, 0, 0);">'white'</span>){ <span style="color: rgb(0, 42, 84);">require</span>(grid) <span style="color: rgb(0, 42, 84);">theme_classic</span>(...) + <span style="color: rgb(0, 42, 84);">theme</span>(<span style="color: rgb(0, 84, 0);">rect</span>=<span style="color: rgb(0, 42, 84);">element_rect</span>(<span style="color: rgb(0, 84, 0);">fill</span>=bg), <span style="color: rgb(0, 84, 0);">plot.margin</span>=<span style="color: rgb(0, 42, 84);">unit</span>(<span style="color: rgb(0, 42, 84);">rep</span>(<span style="color: rgb(184, 93, 0);">0.5</span>,<span style="color: rgb(184, 93, 0);">4</span>), <span style="color: rgb(209, 0, 0);">'lines'</span>), <span style="color: rgb(0, 84, 0);">panel.background</span>=<span style="color: rgb(0, 42, 84);">element_rect</span>(<span style="color: rgb(0, 84, 0);">fill</span>=<span style="color: rgb(209, 0, 0);">'transparent'</span>, <span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'black'</span>), <span style="color: rgb(0, 84, 0);">panel.border</span>=<span style="color: rgb(0, 42, 84);">element_rect</span>(<span style="color: rgb(0, 84, 0);">fill</span>=<span style="color: rgb(209, 0, 0);">'transparent'</span>, <span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'transparent'</span>), <span style="color: rgb(0, 84, 0);">panel.grid</span>=<span style="color: rgb(0, 42, 84);">element_blank</span>(), <span style="color: rgb(0, 84, 0);">axis.title</span> = <span style="color: rgb(0, 42, 84);">element_text</span>(<span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'black'</span>, <span style="color: rgb(0, 84, 0);">vjust</span>=<span style="color: rgb(184, 93, 0);">0.1</span>), <span style="color: rgb(0, 84, 0);">axis.ticks.length</span> = <span style="color: rgb(0, 42, 84);">unit</span>(-<span style="color: rgb(184, 93, 0);">0.4</span>,<span style="color: rgb(209, 0, 0);">"lines"</span>), <span style="color: rgb(0, 84, 0);">axis.ticks</span> = <span style="color: rgb(0, 42, 84);">element_line</span>(<span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'black'</span>), <span style="color: rgb(0, 84, 0);">axis.ticks.margin</span> = <span style="color: rgb(0, 42, 84);">unit</span>(<span style="color: rgb(184, 93, 0);">0.8</span>,<span style="color: rgb(209, 0, 0);">"lines"</span>), <span style="color: rgb(0, 84, 0);">legend.title</span>=<span style="color: rgb(0, 42, 84);">element_blank</span>(), <span style="color: rgb(0, 84, 0);">legend.key</span>=<span style="color: rgb(0, 42, 84);">element_rect</span>(<span style="color: rgb(0, 84, 0);">fill</span>=<span style="color: rgb(209, 0, 0);">'transparent'</span>, <span style="color: rgb(0, 84, 0);">color</span>=<span style="color: rgb(209, 0, 0);">'transparent'</span>)) }
自定义的主题可以编入自己的R语言包中,方便调用。如果觉得你的主题很有代表性,那就发给ggplot2的作者,让他在下一版本中加到ggplot2发行版中。比如上面上面函数加入ggplot2后就可以直接调用:
p <span style="color: rgb(84, 0, 84);"><strong><-</strong></span> <span style="color: rgb(0, 42, 84);">qplot</span>(<span style="color: rgb(0, 84, 0);">x</span>=x, <span style="color: rgb(0, 84, 0);">y</span>=y, <span style="color: rgb(0, 84, 0);">color</span>=x, <span style="color: rgb(0, 84, 0);">fill</span>=x, <span style="color: rgb(0, 84, 0);">geom</span>=<span style="color: rgb(0, 42, 84);">c</span>(<span style="color: rgb(209, 0, 0);">'line'</span>,<span style="color: rgb(209, 0, 0);">'point'</span>), <span style="color: rgb(0, 84, 0);">group</span>=<span style="color: rgb(184, 93, 0);">1</span>) + <span style="color: rgb(0, 42, 84);">labs</span>(<span style="color: rgb(0, 84, 0);">title</span>=<span style="color: rgb(209, 0, 0);">'The figure title.'</span>, <span style="color: rgb(0, 84, 0);">xlab</span>=<span style="color: rgb(209, 0, 0);">'Factor'</span>, <span style="color: rgb(0, 84, 0);">ylab</span>=<span style="color: rgb(209, 0, 0);">'Value'</span>) p + <span style="color: rgb(0, 42, 84);">theme_zg</span>() p + <span style="color: rgb(0, 42, 84);">theme_zg</span>(<span style="color: rgb(0, 84, 0);">base_size</span>=<span style="color: rgb(184, 93, 0);">16</span>, <span style="color: rgb(0, 84, 0);">bg</span>=<span style="color: rgb(209, 0, 0);">'gray90'</span>)
本系列博文到此结束。ggplot2还在完善过程中,一些新功能可能不断会实现,感谢H.W的努力。如果想比较透彻的学习,建议最好直接到Github把ggplot2项目克隆下来研究它的代码。本系列文章的例子基于ggplot2 0.93版本,如果发现一些代码运行不正确或效果图有变化,应该是版本不同了。文章后都有sessionInfo信息可参考。
<span style="color: rgb(0, 42, 84);">sessionInfo</span>()
## R version 3.1.0 (2014-04-10) ## 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=zh_CN.UTF-8 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] ggplot2_0.9.3.1 zblog_0.1.0 knitr_1.5 ## ## loaded via a namespace (and not attached): ## [1] colorspace_1.2-4 digest_0.6.4 evaluate_0.5.3 formatR_0.10 ## [5] gtable_0.1.2 highr_0.3 labeling_0.2 MASS_7.3-31 ## [9] munsell_0.4.2 plyr_1.8.1 proto_0.3-10 Rcpp_0.11.1 ## [13] reshape2_1.2.2 scales_0.2.4 stringr_0.6.2 tools_3.1.0
D.求一个n点无向图的最大点集,满足每个点的度数均大于k 正难则反删点法。维护一个set,按照度数从小到大排列,每次取出度数最小的点,看是否满足大于k,若满足则点数最大为当前set中点数;否则删除该点,与该点相连的点度数减1,维护set的单调性F.求排列由多少个环构成 直接按顺序查找并标记即可G.树中每个点有两个权值a和b。求满足a权值和不大于A,且b权值和最大的链 树分治H.最少操作从一个字_删点,使图中最小度数的点最大
本篇博客针对MythType 6.9b 在office 2016中安装过程中提示缺少MPlugin.dll文件无法正常安装,提示缺少MathPage.wll无法运行,产生'53'错误。进行了详细的操作解释,亲测可用。并且提供了针对office 2016的解决方案,使得更容易找到对应路径,解决问题。_mathtype commands 6 for word 2016 和 mathtype commands 2016
本文简单介绍下枚举文件夹中文件的操作。首先,新建对话框应用程序为了查看方便,我们在对话框上添加一个list列表,用于显示我们枚举到的文件为了演示方便,我在工程中新建了一个文件夹MyFolder 该文件夹中文件子文件夹中文件 好了,看我们演示代码头文件private: void UpdateLi_枚举文件夹下所有文件
I use BufferedReader's readLine() method to read lines of text from a socket.There is no obvious way to limit the length of the line read.I am worried that the source of the data can (maliciously or b..._bufferreader行的长度有限制吗
2017年5月9日,云效平台资深研发工程师向禹通过直播分享了《持续集成与持续交付实践之路》。他从云效背景、云效方案、云效价值三个方面进行了分享。他主要分享了持续集成持续交付的解决方案和案例,并且对大型系统如何实现持续集成、持续交付、进行产品迭代发布进行了详细介绍。 以下内容根据直播视频...
环境:spring boot、RedisTemplate将key和value存入Redis,并进行查看时发现查询了相关文章了解到序列化问题,记录一下package com.spring;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyA...
https://blog.csdn.net/doraemon_wu/article/details/51972261Logback将执行日志事件输出的组件称为Appender,实现的Appender必须继承 ch.qos.logback.core.Appender 接口接口如下:package ch.qos.logback.core;import ch.qos.logback.core..._logback appender-ref
前言基于深度学习的目标检测器能够取得成功,很大一部分依赖于完整标注的大规模数据,但实际上,完整标注的数据集是有限的。为了解决这个问题,一种方法是进行迁移学习,源域(source-domain) 中的知识可以作为一种有效的监督,进而泛化到目标域(target-domain) 的学习过程中。但是,小样本目标检测中的迁移学习依然存在以下问题:当目标检测的数据集很小时,使用一般的迁移策略是不太合适的..._lstd框架图讲解
智能车是一种高新技术密集型的新型汽车,以汽车电子为背景涵盖控制、模式识别、传感技术、电子、电气、计算机、机械等多科学的科技创意性设计,一般主要由路径识别、速度采集、角度控制及车速控制等模块组成。可实现自动驾驶、自动变速及自动识别道路等功能,是现代电子产业发展中一项重要的组成部分。在我国,教育部为了加强大学生实践、创新能力和团队合作精神的培养,委托教育部高等学校自动化专业教学指导分委员会主办了每年一度的全国大学生智能汽车竞赛。全国大学生智能汽车竞赛是在竞赛组委会提供的统一汽车模型平台上,使用飞思卡尔半导体公司
转载—Linux 线程调度与优先级设置Linux内核的三种调度策略:1.SCHED_OTHER 分时调度策略,2.SCHED_FIFO实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃3.SCHED_RR实时调度策略,时间片轮转。当进程的时间片用完,系统将重新分配时间片,并置于就绪队列尾。放在队列尾保证了所有具有相同优先级的RR任务的调度公平L..._linux调度策略
MongoDB副本集数据同步方式intial sync,可以理解为全量同步。replication,追同步源的oplog,可以理解为增量同步。下面会详细介绍MongoDB数据同步的实现原理。initial syncSecondary节点当出现如下状况时,需要先进行全量同步。oplog为空。local.replset.minvalid集合里_initialSyncFlag..._mongodb全量同步
转载http://blog.csdn.net/haoel/article/details/2886 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能。make所支持的函数也不算很多,不过已经足够我们的操作了。函数调用后,函数的返回值可以当做变量来使用。函数的调用语法函数调用,很像变量的使用,也是以“$”来标识的,其语法如下:_makefile 写函数