theme:就是与数据无关的图形设置都可归为主题类。ggplot2的默认的绘图主题呈现出一个灰色的样式。
这个是下面的原始图代码做出来的。 # 原始图 p <- ggplot(mpg, aes(displ, hwy) ) p <- p + geom_point(aes(colour = factor(cyl))) p 我们看一下系统主题的设置 theme_gray > theme_gray function (base_size = 12, base_family = \"\") { theme(line = element_line(colour = \"black\ lineend = \"butt\"), rect = element_rect(fill = \"white\ colour = \"black\size = 0.5, linetype = 1), text = element_text(family = base_family, face = \"plain\ 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\ 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\ 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) } element_line(colour = NULL, size = NULL, linetype = NULL, lineend = NULL, color = NULL) Arguments colour line colour line size size linetype line type lineend line end an alias for colour color 2. element_text: Usage element_text(family = NULL, face = NULL, colour = NULL, size = NULL, hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL, color = NULL) Arguments family font family font face (\"plain\face text colour colour text size (in pts) size horizontal justification (in [0, 1]) hjust vertical justification (in [0, 1]) vjust angle (in [0, 360]) angle lineheight line height an alias for colour color 3. element_rect: Usage element_rect(fill = NULL, colour = NULL, size = NULL, linetype = NULL, color = NULL) Arguments fill fill colour colour border colour border size size linetype border linetype an alias for colour color 4. element_blank() 列举一些常用的参数: 参数 line rect text title axis.title axis.title.x axis.title.y axis.text axis.text.x axis.text.y axis.ticks axis.ticks.x 设置内容 所有线属性 所有矩形区域属性 所有文本相关属性 所有标题属性 坐标轴标题 x轴属性 y轴属性 坐标轴刻度标签属性 属性和继承和前面类似,不再重复 坐标轴刻度线 line 继承自 text axis.title axis.title text axis.ticks.y axis.ticks.length axis.ticks.margin axis.line axis.line.x axis.line.y legend.margin legend.key legend.key.size legend.key.height legend.key.width legend.text legend.text.align legend.title legend.title.align legend.position legend.direction legend.box legend.box.just panel.background panel.border panel.margin panel.grid 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 strip.background strip.text strip.text.x strip.text.y 刻度线长度 刻度线和刻度标签之间的间距 坐标轴线 图例边界 图例符号 图例符号大小 图例符号高度 图例符号宽度 图例文字标签 图例文字标签对齐方式 图例标题 图例标题对齐方式 图例位置 图例排列方向 多图例的排列方式 多图例居中方式 绘图区背景 绘图区边框 分面绘图区之间的边距 绘图区网格线 主网格线 次网格线 整个图形的背景 图形标题 图形边距 分面标签背景 分面标签文本 line rect 0为左齐,1为右齐 text left, right, bottom, top, 两数字向量 \"horizontal\" or \"vertical\" center或两数字向量 \"horizontal\" or \"vertical\" rect rect line top, right, bottom, left rect text legend.background 图例背景 legend.justification 居中方式 实例展示: library(ggplot2) library(grid) # 原始图 p <- ggplot(mpg, aes(displ, hwy) ) p <- p + geom_point(aes(colour = factor(cyl))) p ############################################### # ########参数修改 认清那些theme。######### ############################################### # background 的修改 p1 <- p + theme( # panel.background 绘图区域,黄色部分 panel.background = element_rect(fill = \"yellow\ size = 5, colour = \"green\ linetype = \"dotdash\"), # 图中的图例 legend.background = element_rect(fill = \"blue\ size = 0, colour = \"green\ linetype = \"dotdash\"), # 途中红色部分 plot.background = element_rect(fill = \"red\ size = 2, colour = \"blue\ linetype = \"dotted\"), # gird 的修改 panel.grid.major.x 主刻度线的参数 panel.grid.major.x = element_line(colour = \"black\ size = 2, linetype = \"twodash\"), panel.grid.major.y = element_line(colour = \"pink\ size = 2, linetype = \"solid\"), panel.grid.minor.x = element_line(colour = \"black\ size = 1, linetype = \"twodash\"), panel.grid.minor.y = element_line(colour = \"pink\ size = 1, linetype = \"solid\"), # 绘图区域的控制 plot.margin = unit(c(2,0,1,1), \"lines\"), legend.position = \"right\# axes 坐标轴的控制 axis 标签 刻度线 axis.title.x = element_text(colour = \"blue\ size = 20, face = \"bold.italic\" ), axis.title.y = element_text(colour = \"yellow\ size = 20, face = \"plain\"), size = 3, linetype = \"solid\"), axis.ticks.x = element_line(colour = \"blue\ size = 3, linetype = \"solid\" ), axis.ticks.y = element_line(colour = \"blue\ size = 3, linetype = \"solid\") ) p1 最后,绘图的效果,通过颜色区分作图区域,图形效果请忽略。 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务