数据可视化之美第一章 续
2019-08-16 本文已影响0人
煎饼果子再来一套
1.1.6. 图例
guide_colorbar()/guide_colourbar()用于调整连续变量的图例
guide_legend()用于离散变量的图例,也可用于连续变量
图例位置——thmem(legend.position)参数
none(无图例)、left(左边)、right(右边)、bottom(底部)、top(头部)
legend.position也可用两个元素构成的数值向量控制,主要设置图例在图表中所在的具体位置,数值大小一般在0~1,超出数值导致图例隐藏,最好同时设置图例背景(legend.background)为透明或无背景
theme(legend.background = element_rect(fill = 'white'), legend.position = 'right')
# 设置背景为白色填充的矩形,位置设定为图标右侧
theme(legend.background = element_blank, legend.position =c(0.2, 0.8))
# 设置背景为无,位置设定为x轴方向20%,y轴方向80%
1.1.7. 主题系统
1、ggThemeAssit 包
2、套用主题模板
1.1.8. 位置调整
geom_xxx(position=''),position参数
'identity' :无调整,默认,多分类柱形图中不可行,序列间会遮盖
'stack' :垂直堆叠放置(堆积柱形图)
'dodge' : 水平抖动放置(簇状柱形图,position = position_dodge())
'fill' : 百分比化(垂直堆叠放置)
表1-6-7
扫描文稿 1.jpg
N <- 100
df <- data.frame(group = rep(c(1,2),each = N*2),
y = append(append(rnorm(N,5,1),rnorm(N,2,1)),
append(rnorm(N,1,1),rnorm(N,3,1))),
x = rep(c('A','B','A','B'),N))
suppressPackageStartupMessages(library(ggplot2))
par(mfrow = c(1,3))
#未调整箱型图和抖动散点图的间距
ggplot(df,aes(x = x, y = y, fill = as.factor(group)))+
geom_boxplot(outlier.size = 0, colour = 'black')+
geom_jitter(aes(group = as.factor(group)),
shape = 21, alpha = .5)
#调整抖动散点图的间距
ggplot(df,aes(x = x, y = y, fill = as.factor(group)))+
geom_boxplot(outlier.size = 0, colour = 'black')+
geom_jitter(aes(group = as.factor(group)),
shape = 21, alpha = .5,
position = position_jitterdodge())
#同时调整箱型图和抖动散点图的间距
ggplot(df,aes(x = x, y = y, fill = as.factor(group)))+
geom_boxplot(outlier.size = 0, colour = 'black',
position = position_dodge(0.75))+
geom_jitter(aes(group = as.factor(group)),
shape = 21, alpha = .5,
position = position_jitterdodge(dodge.width = 0.75))



ggplot与geom对象之间的关系
ggplot() 具有全局优先级,如geom_xxx()内未指定相关系数,可被之后的所有geom_xxx()对象继承
geom_xxx()内的参数属于局部参数,仅作用于geom_xxx()对象内部