2017-05-20 16:30:23
qplot()ggplot(): 初始化一个ggplot对象geom_<形状>: geom_函数家族整合美学映射函数aes_、统计转换参数stat、位置偏移参数position、其他美学参数width、color等scale_<尺度类型>: scale_函数家族用来指定视觉通道的映射方法coord_<坐标系>: coord_函数家族用来调整坐标系facet_grid/facet_wrap: 指定分面依据guides: 调整尺度和视觉通道的映射关系theme: 低级函数,修改图中每个部件的呈现形式ggtitle, xlab, ylab: 图/坐标轴标题annotate: 图上加注qplot用来快速生成统计图,但自定义弹性不大
qplot(x, y = NULL, ..., data, facets = NULL, margins = FALSE, geom = "auto", xlim = c(NA, NA), ylim = c(NA, NA), log = "", main = NULL, xlab = deparse(substitute(x)), ylab = deparse(substitute(y)), asp = NA, stat = NULL, position = NULL)
指定连续变量x,y,自动适配散点图
library(ggplot2) qplot(displ, hwy, data = mpg)

qplot(displ, hwy, data = mpg, geom = c("point", "smooth"))

将class映射到颜色通道,drv映射到形状
qplot(displ, hwy, data = mpg, shape = drv, color = class)

qplot(displ, hwy, data = mpg, facets = . ~ drv, margins = TRUE)

ggplot)ggplot(data = NULL, mapping = aes(), ..., environment = parent.frame())
fortify函数预处理aes函数传入ggplot(df, aes(x, y, <other aesthetics>))ggplot(df)ggplot(): 如果一幅图要用到多个独立的数据源,data可留空geom_)geom_<形状>(mapping = NULL, data = NULL, stat, position, ..., na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE)
aes或aes_函数指定。一般inherit.aes为TRUE,即从初始化的ggplot对象直接继承美学映射参数。position_stack, position_dodgeaes 美学/视觉通道映射: 映射谁?aes(x, y, ...)
- x: x轴/rho变量 - y: y轴/theta变量 - xmin: x最小值 - xmax: x最大值 - ymin: y最小值 - ymax: y最大值 - xend: x终点 - yend: y终点 - alpha: 透明度 - colour: 边线色
- fill: 填充色 - group: 分组 - linetype: 线型 - size: 粗细/大小 - xintercept: x截距 - yintercept: y截距 - slope: 斜率 - weight: 权重 - shape: 形状 - stroke: 描边 - map_id: 地图多变性id
scale_): 怎么映射?scale_<视觉通道>_<映射方法>(...)
coord_)coord_cartesian: 直角平面坐标系coord_fixed: 固定长宽比的直角坐标系
coord_equal: 特例coord_flip: 颠倒x/ycoord_map: 地图坐标系, projection指定投影方法
coord_quickmap: 快捷地图坐标系coord_polar: 极坐标系coord_trans: 自定义调整坐标系facet_)facets必须是公式(行~列)facet_grid(facets, margins = FALSE, scales = "fixed", space = "fixed", shrink = TRUE, labeller = "label_value", as.table = TRUE, switch = NULL, drop = TRUE)
facets可以是公式(~a + b,)或文本向量(c("a", "b"))facet_wrap(facets, nrow = NULL, ncol = NULL, scales = "fixed", shrink = TRUE, labeller = "label_value", as.table = TRUE, switch = NULL, drop = TRUE, dir = "h", strip.position = "top")
guides调整尺度效果guides可以在定义尺度时一并定义,也可单独用guides函数统一定义
实现同一效果:
p + guides(colour = "colorbar", size = "legend", shape = "legend")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")theme调整图形部件theme(line, rect, text, title, aspect.ratio, axis.title, axis.title.x, axis.title.x.top, axis.title.y, axis.title.y.right, axis.text, axis.text.x, axis.text.x.top, axis.text.y, axis.text.y.right, axis.ticks, axis.ticks.x, axis.ticks.y, axis.ticks.length, axis.line, axis.line.x, axis.line.y, legend.background, legend.margin, legend.spacing, legend.spacing.x, legend.spacing.y, 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.justification, legend.box, legend.box.just, legend.box.margin, legend.box.background, legend.box.spacing, panel.background, panel.border, panel.spacing, panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.subtitle, plot.caption, plot.margin, strip.background, strip.placement, strip.text, strip.text.x, strip.text.y, strip.switch.pad.grid, strip.switch.pad.wrap, ..., complete = FALSE, validate = TRUE)
各部件可调用对应的element_函数进行编辑: element_line, element_rect, element_text
theme_套用主题theme()自制主题
theme_bw: 白底主题theme_classic: 经典主题theme_dark: 夜色主题theme_gray/theme_grey: (默认)灰色主题theme_light: 简洁主题theme_linedraw: 描线主题theme_minimal: 最小化主题theme_void: 极简主题ggthemes包提供了包括更多主题,如excel, gdocs, economist, wsjggtitle, xlab, ylab定义标题labs(...): 定义title, caption, subtitle及美学属性xlab(label) / ylab(lable)/ ggtitle(label, subtitle = NULL)annotate定义注释annotate(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL, ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ..., na.rm = FALSE)
segment, pointrange同样的例子,用逐步法实现
p = ggplot(mpg, aes(displ, hwy)) p # 仅初始化,什么都不显示

将class映射到颜色通道,drv映射到形状
p = p + geom_point(aes(shape=drv, color=class)) + geom_smooth(method="loess") p

调整映射方法
p1 = p + scale_shape_manual(values=c("4"=1, "f"=2, "r"=3)) + scale_color_brewer()
p1

p = p + facet_grid(~drv) p

p + theme_light()

p + theme(panel.background=element_blank(), panel.grid.major.y=element_line(color="gray"),
axis.line.x=element_line(color="darkgray")) +
guides(colour = guide_legend("Car Type", order=1), shape = guide_legend("Driver"))

散点图: 两变量相关性
ggplot(mtcars, aes(wt, mpg)) + geom_point()

文本型变量作散点图
ggplot(OrchardSprays, aes(treatment, decrease)) +
geom_point()

直方图: 单变量密度分布
ggplot(mtcars, aes(mpg)) + geom_histogram(bins=9)

或用密度图
ggplot(mtcars, aes(mpg)) + geom_density()

连续性变量的分布
ggplot(ToothGrowth, aes("", len)) + geom_boxplot()

多个箱式图
ggplot(ToothGrowth, aes(dose, len,
color=factor(dose))) + geom_boxplot()

分类变量的比较
hp <- data.frame(hp=mtcars$hp,
names = row.names(mtcars))
ggplot(hp, aes(names, hp)) + geom_col()

自变量较多时用横向条图
ggplot(hp, aes(names, hp)) +
geom_bar(stat="identity") + coord_flip()

ts类型数据要先转换
AP <- data.frame(n=as.numeric(AirPassengers),
t=zoo::as.Date(time(AirPassengers)))
ggplot(AP, aes(t, n)) + geom_line()

阶梯折线
ggplot(AP, aes(t, n)) + geom_step()

需要用coord_polar
admit <- data.frame(Admit=c("Admitted", "Rejected"),
n=apply(UCBAdmissions, 1, sum))
ggplot(admit, aes("", n, fill=Admit)) +
geom_bar(stat="identity", position="stack",
width=1) + coord_polar(theta="y")

玫瑰图: position='dodge'
ggplot(admit, aes("", n, fill=Admit)) +
geom_bar(stat="identity", position="dodge",
width=1) + coord_polar()

Thank you!