图例函数: * guide_colorbar() /guide_colourbar() 用于连续变量的图例 * guide_legend() 用于离散变量的图例,也可以用于连续变量 * guides() 将_colorbar和_legend嵌套进去,方便映射,如guides(fill = guide_colorbar()) 可以在scale_xxx() 标度中指定guide类型,guide = “colorbar”或guide = “legend” 常用公共参数: (点击放大)9.1 guide_colorbar**_colorbar()参数: ** (点击放大)library(ggplot2) library(reshape2) df <- melt(outer(1:4, 1:4), varnames = c('X1', 'X2'))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value)) p2 <- p1 + geom_point(aes(size = value))
p1 + scale_fill_continuous(guide = 'colorbar') # 默认形式 p1 + guides(fill = guide_colorbar()) # 具体映射
p1 + scale_fill_distiller(palette = 'YlGn', direction = 1) + guides(fill = guide_colorbar(title = '值', nbin = 100, # 指定图例名称,水平放置,增加分箱数为100 barwidth = 0.5, barheight = 10,# 指定图例箱体尺寸,宽为0.5mm,高为10mm ticks.colour = 'red', # 更改刻度线颜色 frame.colour = 'blue',frame.linewidth = 0.5, # 增加箱体边框 draw.ulim = TRUE, draw.llim = TRUE # 显示最大,最小刻度线 )) p2 + scale_fill_continuous(guide = 'colorbar') + scale_size(guide = 'legend') # 在标度中控制图例 p2 + guides(fill = 'colorbar', size = 'legend') # 与上面结果一样
p2 + scale_fill_continuous(guide = guide_colorbar(direction = 'horizontal')) + scale_size(guide = guide_legend(direction = 'vertical')) # 更改图例方向
9.2 guide_legend**_legend()参数:** (点击放大)library(ggplot2) library(reshape2) df <- melt(outer(1:4, 1:4), varnames = c('X1', 'X2')) p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value)) p2 <- p1 + geom_point(aes(size = value))
p1 + scale_fill_continuous(guide = guide_legend()) # 连续标度中设置离散图例 p1 + scale_fill_distiller(type = 'qual', palette = 'Set3') + guides(fill = guide_legend(title = '左', title.position = 'left', # 指定图例名称为'左', 位置为箱体的左边 key.width = 5, key.height = 10, nrow = 2, ncol = 2, byrow = TRUE # 修改箱体尺寸,并矩形排列,按行排 ))
p1 + guides(fill = guide_legend( title.theme = element_text(size = 15, face = 'italic', colour = 'red', angle = 0)) # 在图例中修改图例主题,一般在主题函数内修改 )
p1 + scale_fill_continuous(breaks = c(5, 10, 15), labels = paste('long', c(5, 10, 15)), guide = guide_legend( direction = 'horizontal', # 水平排列箱体 title.position = 'top', # 图例标题置于顶部 label.position = 'bottom', # 图例刻度标签置于底部 label.hjust = 0.5, # 刻度标签水平位置偏移 label.vjust = 1, # 刻度标签垂直位置偏移 label.theme = element_text(angle = 90) # 图例主题中修改刻度标签角度 ) )
9.3 guides多个图例guides多个图例:
guides() 内部嵌套guide_legend() 和guide_colorbar() ,添加一个映射参数,如: guides( colour = guide_colourbar(order = 1), shape = guide_legend(order = 2), size = guide_legend(order = 3) )
library(ggplot2) dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5), r = factor(1:5)) p <- ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) + geom_point() p
p + guides(colour = guide_colorbar(), size = guide_legend(), shape = guide_legend()) # 默认按参数顺序排列多个图例 p + scale_colour_continuous(guide = 'colorbar') + scale_size_discrete(guide = 'legend') + scale_shape(guide = 'legend') + guides(colour = 'none') # 删除一个图例
# 设定多个图例 ggplot(mpg, aes(displ, cty)) + geom_point(aes(size = hwy, colour = cyl, shape = drv)) + guides( colour = guide_colourbar(order = 1), # order指定图例排列顺序 shape = guide_legend(order = 2), size = guide_legend(order = 3) )
9.4 多图例合并library(ggplot2) # 图例合并: 多个不同标度图例合并: # 当图例类型一致,图例标题一致时,会自动合并图例 dat <- data.frame(x = 1:5, y = 1:5, p = 1:5, q = factor(1:5), r = factor(1:5)) p <- ggplot(dat, aes(x, y, colour = p, size = q, shape = r)) + geom_point() p + guides(colour = guide_legend('这是图例标题'), size = guide_legend('这是图例标题'), shape = guide_legend('这是图例标题')) + theme(legend.position = 'bottom') # 主题函数中调节图例位置
## 多种几何对象图例合并: ## 若都是同一个变量映射的,且标度类型一致,标度标题相同,标度values等长,给标度新增labels参数,labels相同,则会自动合并图例 state1 <- c(rep(c(rep('N', 7), rep('Y', 7)), 2)) year <- rep(c(2003:2009), 4) group1 <- c(rep('C', 14), rep('E', 14)) group2 <- paste(state1, group1, sep = '') beta <- c(0.16, 0.15, 0.08, 0.08, 0.18, 0.48, 0.14, 0.19, 0, 0, 0.04, 0.08, 0.27, 0.03, 0.11, 0.12, 0.09, 0.09, 0.1, 0.19, 0.16, 0, 0.11, 0.07, 0.08, 0.09, 0.19, 0.1) lcl <- c(0.13, 0.12, 0.05, 0.05, 0.12, 0.35, 0.06, 0.13, 0, 0, 0.01, 0.04, 0.2, 0, 0.09, 0.09, 0.06, 0.06, 0.07, 0.15, 0.11, 0, 0.07, 0.03, 0.05, 0.06, 0.15, 0.06) ucl <- c(0.2, 0.2, 0.13, 0.14, 0.27, 0.61, 0.28, 0.27, 0, 1, 0.16, 0.16, 0.36, 0.82, 0.14, 0.15, 0.13, 0.13, 0.15, 0.23, 0.21, 0, 0.15, 0.14, 0.12, 0.12, 0.23, 0.16) data <- data.frame(state1, year, group1, group2, beta, lcl, ucl)
ggplot(data = data, aes(x = year, y = beta, colour = group2, shape = group2)) + geom_point(size = 4) + geom_errorbar(aes(ymin = lcl, ymax = ucl), colour = 'black', width = 0.5) + scale_colour_manual(name = 'Treatment & State', labels = c('Control, Non-F', 'Control, Flwr', 'Exclosure, Non-F', 'Exclosure, Flwr'), values = c('blue', 'red', 'blue', 'red')) + scale_shape_manual(name = 'Treatment & State', labels = c('Control, Non-F', 'Control, Flwr', 'Exclosure, Non-F', 'Exclosure, Flwr'), values = c(19, 19, 17, 17))
### 映射变量相同,在标度labs函数中设置相同的标度名称 ggplot(iris) + aes(x = Sepal.Length, y = Sepal.Width, color = Species, linetype = Species, shape = Species) + geom_line() + geom_point() + labs(color = 'Guide name', linetype = 'Guide name', shape = 'Guide name')
### 下一个例子 x <- seq(0, 10, by = 0.2) y1 <- sin(x) y2 <- cos(x) y3 <- cos(x + pi/4) y4 <- sin(x + pi/4) df1 <- data.frame(x, y = y1, Type = as.factor('sin'), Method = as.factor('method1')) df2 <- data.frame(x, y = y2, Type = as.factor('cos'), Method = as.factor('method1')) df3 <- data.frame(x, y = y3, Type = as.factor('cos'), Method = as.factor('method2')) df4 <- data.frame(x, y = y4, Type = as.factor('sin'), Method = as.factor('method2'))
df.merged <- rbind(df1, df2, df3, df4)
y5 <- sin(x - pi/4) df5 <- data.frame(x, y = y5, Type = as.factor('sin'), Method = as.factor('method3')) df.merged <- rbind(df1, df2, df3, df4, df5)
df.merged$int <- paste(df.merged$Type, df.merged$Method, sep = '.') # 给数据源新增一列变量
ggplot(df.merged, aes(x, y, colour = int, linetype = int, shape = int)) + geom_line() + geom_point() + scale_colour_discrete('') + scale_linetype_manual('', values = c(1, 2, 1, 2, 3)) + scale_shape_manual('', values = c(17, 17, 16, 16, 15))
9.5 新增坐标轴所有的新增坐标轴都是基于现有坐标轴变换而来的 * sec_axis(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver()) * dup_axis(trans = ~., name = derive(), breaks = derive(), labels = derive()) * derive() 参数解释: * trans 表示指定变换公式 * name 表示指定新增坐标轴的名称 * breaks 表示指定新增坐标轴刻度点位置 * labels 表示指定新增坐标轴刻度标签 * derive 表示继承现有坐标轴,基本上就是复制,没有变换关系,如果有变换关系,还是用sec_axis() 吧 library(ggplot2) p <- ggplot(mtcars, aes(cyl, mpg)) + geom_point() p + scale_y_continuous(sec.axis = sec_axis(~. + 10)) # 在标度函数中新增第2个y轴,变换关系为:原y轴 + 10 p + scale_y_continuous('英里/每加仑', sec.axis = sec_axis(~. + 10, name = '公里/L')) # 新增y轴,轴名称为:公里每升,原y轴为:英里/加仑 p + scale_y_continuous(sec.axis = ~.^2) # 变换关系为:平方 p + scale_y_continuous(sec.axis = ~.^2 * 3 + 4 * .) # 变换关系为:3*y^2 + 4*y
10 themes主题系统内部函数及参数非常多,跟present data没有一点关系,新手不建议学,会搅乱思路 针对新手,建议使用ggThemeAssist包进行主题设置,用鼠标而不是代码,更加方便,或者套用主题模板,下面会介绍 10.1 主题函数分类新手可以跳过这个小节
主题系统_文本标签: * element_text() * element_rect() * element_line() * element_blank() 清空任意主题对象,默认返回默认主题
10.2 ggThemeAssist包需要安装shiny包 安装好该包后,RStudio界面就会出现“Addins”下拉菜单 使用方法:首先运行函数要画图的ggplot2代码,以加载到内存 然后选中该画图函数,如gg, 然后点击Addins下拉菜单, 点击“ggplot Theme Assistant”,就会出现一个交互式的shiny弹窗,然后在该弹窗上用鼠标操作 在交互弹窗中处理完后,点击右上角的“Done”按钮,然后就将主题代码输出到需要的位置了 最后进行对代码进行微调,有的地方可能会少括号或引号 如图所示,真的非常简单
rm(list = ls()) gc() library(ggplot2) gg <- ggplot(mtcars, aes(x = hp, y = mpg, colour = as.factor(cyl))) + geom_point() + theme_bw() # 运行以上代码,然后选中下一行代码中的'gg'函数, gg + theme(plot.subtitle = element_text(size = 12, face = 'bold', colour = 'maroon2', hjust = 0.75), plot.caption = element_text(size = 10, face = 'bold', colour = 'mediumpurple1'), axis.line = element_line(colour = 'darkorchid', size = 0.3, linetype = 'solid'), axis.ticks = element_line(colour = 'magenta', size = 1.5), panel.grid.major = element_line(linetype = 'blank'), panel.grid.minor = element_line(linetype = 'blank'), axis.title = element_text(size = 13, face = 'bold', colour = 'brown1'), axis.text = element_text(family = 'serif', size = 13, face = 'bold', colour = 'orangered', hjust = 1, vjust = 0.25, angle = 20), axis.text.x = element_text(colour = 'firebrick1'), plot.title = element_text(family = 'serif', size = 16, face = 'bold', colour = 'deeppink', hjust = 0.5), legend.text = element_text(family = 'mono', colour = 'brown2'), legend.title = element_text(face = 'bold', family = 'mono', colour = 'darkgreen'), panel.background = element_rect(fill = 'goldenrod1', colour = 'white', linetype = 'solid'), plot.background = element_rect(fill = 'chartreuse1', colour = NA), legend.key = element_rect(fill = 'gray17'), legend.background = element_rect(fill = 'lemonchiffon'), legend.position = 'top', legend.direction = 'horizontal') + labs(title = 'my标题', x = '这是x轴标题', y = '这是y轴标题', subtitle = '这是副标题', caption = '这是caption尾注') # 选中该代码,然后点击Addins下拉菜单, 在shiny弹窗中进行操作
10.3 套用主题模板主题模板包,包括ggthemes, ggtech, ggthemer, ggsci, 有很多种风格: 对于著名科技公司,如谷歌,twiter等 著名可视化软件,如D3,Tableau等, 还有著名学术期刊,如SCI,柳叶刀等 还有著名出版物,如华尔街日报等 总之,应有尽有 在套用主题模板前,先看几个主题修改函数: * theme() 主题函数,自定义主题 * theme_get() 获取当前默认主题的所有参数(激活主题) * theme_set(new) 设置新主题(同时静默返回旧主题以便还原系统默认主题) * theme_update() theme()函数内部的参数会替换theme_get()内部的同名参数, theme_update()直接作用于当前主题 * theme_replace() theme_replace()等价于theme_get() %+replace% theme(), theme()函数内部的参数会替换theme_get()内部的同名参数,未声明的参数全部初始化为NULL。 下次使用主题时,就会更新时才调用这次更新的theme()参数 * e1% + replace% e2 系统预设主题: * theme_grey /theme_gray * theme_bw * theme_linedraw * theme_light * theme_dark * theme_minimal * theme_classic * theme_void 具体见章节:颜色及主题模板
|