采用图层的设计方式,有利于结构化思维
将表征数据和图形细节分开,能快速将图形表现出来,使创造性绘图更加容易,而不必纠结于图形的细节,细节可以后期慢慢调整
将常见的统计变换融入到了绘图中
有明确的起始(ggplot开始)与终止(一句话一个图层),图层之间的叠加是靠“+”实现的,越往后,其图层越在上方
图形美观,扩展包丰富,有专门调整字体和公式的包,有专门调整颜色的包,还有专门用按钮辅助调整主题的包,总之,应有尽有
Data数据, Mapping映射
Scale标度
Geometric几何对象
Statistics统计变换
Coordinate坐标系统
Layer图层
Facet分面
Legend图例
beautiful美化
ggplot(data, aes(x = , y = )) + # 基础图层,不出现任何图形元素,
geom_xxx()|stat_xxx() + # 几何图层或统计变换,出现图形元素
coord_xxx() + # 坐标变换,默认笛卡尔坐标系
scale_xxx() + # 标度调整,调整具体的标度
facet_xxx() + # 分面,将其中一个变量进行分面变换
guides() + # 图例调整
theme() # 主题系统
ggplot(data = NULL, mapping = aes())
geom_xxx(data = NULL, mapping = aes())
ggplot()内有data、mapping两个参数
具有全局优先级,可以被之后的所有geom_xxx对象或stat_xxx()所继承(前提是geom或stat未指定相关参数)
而geom_xxx()或stat_xxx()内的参数属于局部参数,仅仅作用于内部
为了避免混乱,通常将共性映射的参数指定在ggplot(aes())aes内部,将个性映射的参数指定在geom_xxx(aes())或stat_xxx(aes())内部
几何对象geom_xxx(stat = ) 内有统计变换参数stat,统计变换stat_xxx(geom = )内也有几何对象参数geom
两种方法结果相同,几何对象更专注于结果,统计变换更专注于变换过程
library(ggplot2)
# 用几何对象作图
ggplot(data = NULL, mapping = aes(x = x, y = y)) + geom_point(color = "darked",
stat = "identity") # identity 表示没有任何统计变换
# 用统计变换作图
ggplot(data = NULL, mapping = aes(x = x, y = y)) + stat_identity(color = "darked",
geom = "point") # geom_point(stat = 'identity')与stat_identity(geom = 'point')结果一样
aes参数用来指定要映射的变量,可以是多个变量,
data参数表示指定数据源,必须是data.frame格式,其坐标轴变量最好宽转长,只能指定一个x轴和y轴,多个x列或y列不能使用调整图例。
常用的几种几何对象函数:
几何对象函数 | 描述 | 其它 |
---|---|---|
geom_point | 点图 | geom_point(position = “jitter”) == geom_jitter() 避免重叠 |
geom_line | 折线图 | 可以通过smooth参数平滑处理 |
geom_bar | 柱形图 | x轴是离散变量 |
geom_area | 面积图 | |
geom_histogram | 直方图 | x轴数据是连续的 |
geom_boxplot | 箱线图 | |
geom_rect | 二维长方形图 | |
geom_segment | 线段图 | |
geom_path | 几何路径 | 由一组点按顺序连接 |
geom_curve | 曲线 | |
geom_abline | 斜线 | 有斜率和截距指定 |
geom_hline | 水平线 | 常用于坐标轴绘制 |
geom_vline | 竖线 | 常用于坐标轴绘制 |
geom_text | 文本 |
ggplot2唯一不支持的常规平面图形是雷达图
其它几何对象查询:
ggplot2 part of the tidyverse
ggplot2 Quick Reference: geom
也可以用
ls(pattern = ‘^geom_’, env = as.environment(‘package:ggplot2’)) 查询,但是没有图形示例
library(ggplot2)
ls(pattern = "^geom_", env = as.environment("package:ggplot2"))
柱形图和散点图是关键,并且与极坐标变换紧密相连,着重介绍柱形图和散点图,其它的原理和参数都类似
能用作变量映射的包括:
美学参数 | 描述 |
---|---|
color/col/colour | 指定点、线和填充区域边界的颜色 |
fill | 指定填充区域的颜色,如条形和密度区域, 第21到24号点也有填充色 |
alpha | 指定颜色的透明度,从0(完全透明) 到 1(不透明) |
size | 指定点的尺寸或线的宽度,单位为mm |
angle | 角度,只有部分几何对象有,如geom_text文本的摆放角度, geom_spoke中短棒摆放角度 |
linetype | 指定线条的类型 |
shape | 点的形状, 为[0, 25]区间的26个整数 |
vjust | 垂直位置微调,在(0, 1)区间的数字或位置字符串: 0=“buttom”, 0.5=“middle”, 1=“top” , 区间外的数字微调比例控制不均 |
hjust | 水平位置微调,在(0, 1)区间的数字或位置字符串:0=“left”, 0.5=“center”, 1=“right” , 区间外的数字微调比例控制不均 |
不常映射的参数 | 描述 |
---|---|
binwidth | 直方图的宽度 |
notch | 表示方块图是否应为缺口 |
sides | 表示地毯图的安置位置(“b”底部, “l”左部, “t”顶部, “r”右部, “bl”左下角, 等等) |
width | 箱线图或柱形图的宽度,从(0, 1), 柱形图默认0.9即90% |
lineend | 表示指定宽线条端部形状,有3种:“round”半圆形,“square”增加方形, “butt”默认不变, 常用于geom_path和geom_line几何对象 |
family | 字体(Font face),内置的只有3种:“sans”, “serif”, “mono” |
fontface | 字型,分为: “plain”常规体, “bold”粗体, “italic”斜体, “bold.italic”粗斜体。常用于geom_text等文本对象 |
lineheight | 长文本换行行距, 常用于geom_text等文本对象 |
R自身自持很多种方式的颜色,“颜色名称”和“HEX色值”最常用和方便,其它的需要扩展包
颜色名称如:
“white”, “azure”, “bisque”, “blue”, “black”, “brown”, “chacolate”, “coral”, “cornsilk”, “cyan”, “gold” ,
“darkgolden”, “orange”, “orchild”, “gray”, “grey”, “tomato”, “violet”, “wheat”, “yellow”, “pink”,
“purple”, “red”, “salmon”, “seashell”, “ivory”,“magentia”,“navy”等系列。
所有的颜色名称见: R_Color_Chart(后台回复:颜色,可下载PDF版本)
如果对一组颜色进行映射的话,建议使用RColorBrewer等调色包,更加方便
RColorBrewer颜色板如下,左边为字符串编号,上下分为3个版块,分别为渐变色板Sequential,离散对比色板Qualitative,两极色板Diverging
# colors() # 调用所有内置颜色编号,名称
scales::show_col(colors()[1:6]) # show_col函数可以将颜色名称或HEX色值向量显示出来
# RColorBrewer包使用
library("RColorBrewer")
display.brewer.all() # 显示所有可用色板
display.brewer.all(type = "seq") # 查看渐变色板
RColorBrewer使用方法:
通过函数brewer.pal(n, name)抽取色条名字为name的n种颜色,后面还可以用“[]”索引符号索取色块,
一个几何对象设置多种颜色只能在标度中设置,我们会在标度中继续讲解,例:
library("RColorBrewer")
display.brewer.pal(7, "PuRd") # 抽取PuRd色条7种颜色,其颜色色值范围没有变,只是色值间隔增大了
display.brewer.pal(9, "PuRd")[11] # 抽取PuRd色条11种颜色,其颜色色值范围没有变,指定色值间隔减小了
线条形状通过名称或整数指定:
线型 | 描述 |
---|---|
0=“blank” | 白线 |
1=“solid” | 实线 |
2=“dashed” | 短虚线 |
3=“dotted” | 点线 |
4=“dotdash” | 点横线 |
5=“longdash” | 长虚线 |
6=“twodash” | 短长虚线 |
自定义线型
通过1个十六进制的字符串来自定义,字符串长度为2、4、6或8。
第1个数字为代表闭合的长度,第2个数字代表缺口的长度,第3个数字又是闭合的长度,第4个数字是缺口的长度,如此交替排列。 然后作为一个整体重复排列
如:
library(ggplot2)
lty <- c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash")
linetypes <- data.frame(
y = seq_along(lty), # seq_along表示生成与对象同样长度的序列
lty = lty
)
ggplot(linetypes, aes(0, y)) +
geom_segment(aes(xend = 5, yend = y, linetype = lty)) + # 将一个变量映射到线型
scale_linetype_identity() +
geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
scale_x_continuous(NULL, breaks = NULL) +
scale_y_reverse(NULL, breaks = NULL)
# 自定义线型
lty <- c("33", "18", "81", "f8", "5f", "357a", "5a73", "342167", "a23f45b6") # 自定义9种线型
linetypes <- data.frame(
y = seq_along(lty),
lty = lty
)
ggplot(linetypes, aes(0, y)) +
geom_segment(aes(xend = 5, yend = y, linetype = lty)) + # 将一个变量映射到线型
scale_linetype_identity() +
geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
scale_x_continuous(NULL, breaks = NULL) +
scale_y_reverse(NULL, breaks = NULL)
[0, 25]个序号代表26种点型, 只有21到26号点形能fill颜色,其它都只有轮廓颜色,
序号32:127对应ASCII字符
所有序号如图所示:
library("ggplot2")
d = data.frame(p = c(0:25, 32:127))
ggplot() + scale_y_continuous(name = "") + scale_x_continuous(name = "") + scale_shape_identity() +
geom_point(data = d, mapping = aes(x = p%%16, y = p%/%16, shape = p), size = 5,
fill = "red") + geom_text(data = d, mapping = aes(x = p%%16, y = p%/%16 +
0.25, label = p), size = 3)
内置的只有3种,可以通过扩展包extrafont来将其它字体转换为ggplot2可识别的标准形式 还可以通过showtext包以图片的形式将字体插入到ggplot2图中,对于公式之类的特殊字体特别方便,比Latex容易爆了
library("ggplot2")
df <- data.frame(x = c(0.4, 0.6, 0.8), y = 1:3, family = c("sans", "serif",
"mono"))
ggplot(df, aes(x, y)) + geom_text(aes(label = family, family = family), size = 15) +
xlim(0.2, 1) + ylim(0, 4)
分为4类:“plain”常规体, “bold”粗体, “italic”斜体, “bold.italic”粗斜体
library("ggplot2")
df <- data.frame(x = c(1, 1.5, 3, 3.5), y = c(2, 1, 1, 2), fontface = c("plain",
"bold", "italic", "bold.italic"))
ggplot(df, aes(x, y)) + geom_text(aes(label = fontface, fontface = fontface),
size = 15) + xlim(0, 5.5) + ylim(0.5, 2.5)
vjust: “top” = 1, “middle” = 0.5, “bottom” = 0
hjust: “left” = 0, “center” = 0.5, “right” = 1
微调后,该几何对象还是在另一个几何对象周围
library("ggplot2")
just <- expand.grid(hjust = c(0, 0.5, 1), vjust = c(0, 0.5, 1))
just$label <- paste0(just$hjust, ", ", just$vjust)
ggplot(just, aes(hjust, vjust)) + geom_point(colour = "grey70", size = 5) +
geom_text(aes(label = label, hjust = hjust, vjust = vjust)) # 也能进行映射,但很少用
包括:
位置调整函数 | 描述 | 其它 |
---|---|---|
position_dodge() | 水平并列放置 | position_dodge(width = NULL, preserve = c(“total”, “single”)) 簇状柱形图 |
position_dodge2() | 水平并列放置 | position_dodge2(…, padding = 0.1, reverse = FALSE) 簇状柱形图,多了2个参数 |
position_identity() | 位置不变 | 对于散点图和折线图,可行,默认为identity,但对于多分类柱形图,序列间存在遮盖 |
position_stack() | 垂直堆叠 | position_stack(vjust = 1, reverse = FALSE) 柱形图和面积图默认stack堆积 |
position_fill() | 百分比填充 | position_fill(vjust = 1, reverse = FALSE) 垂直堆叠,但只能反映各组百分比 |
position_jitter() | 扰动处理 | position_jitter(width = NULL, height = NULL, seed = NA)部分重叠, 用于点图 |
position_jitterdodge() | 并列抖动 | position_jitterdodge(jitter.width = NULL,jitter.height = 0, dodge.width = 0.75,seed = NA) |
position_nudge() | 整体位置微调 | position_nudge(x = 0, y = 0),整体向x和y方向平移的距离,常用于geom_text文本对象 |
position_xxx()内其它参数:padding, preserve, reverse, vjust, width, height 等
参数名称 | 值 | 描述 |
---|---|---|
preserve | c(“total”, “single”) | 当同一组柱子高度相同时,是保留单个柱子,还是保留所有柱子 |
padding | 数字,(0, 1)区间 | 调整柱子间距(中心距离), 越大,则柱子宽度缩小越多, 间距越大,0.5表示宽度缩小50%以增大间距 |
reverse | TRUE/FALSE | 是否翻转各组柱子内部的排列顺序,对于dodge2则水平顺序翻转,对于stack和fill则垂直顺序不同 |
vjust | (0,1)区间 | 调整点和线的垂直位置,默认1顶部,0.5表示居中,0表示处于底部,折线的变化趋势会变平缓,默认1 |
width | (0,1)区间 | 表示水平抖动的程度,因为存在正负方向,所有抖动的范围为其2倍,默认0.5 |
height | (0,1)区间 | 表示垂直抖动的程度,因为存在正负方向,所以抖动的范围为其2倍, 默认0.5 |
dodge.width | (0,1)区间 | 表示各组的点抖动总的水平宽度,默认0.75, 表示点分布在各组箱子75%宽度上 |
library(ggplot2)
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = "dodge2") # 水平并列柱形图,默认保留所有柱子
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = position_dodge(preserve = "total")) # 保留所有柱子
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = position_dodge(preserve = "single")) # 保留单个柱子
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = position_dodge2(preserve = "single",
reverse = T)) # 翻转各组柱子内部排列顺序
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = position_dodge2(preserve = "single",
padding = 0.5)) # 所有柱子宽度缩小50%
library(ggplot2)
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar() # 柱形图默认stack堆积
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = "fill") # 百分比堆积
ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + geom_bar(position = position_stack(reverse = TRUE)) # 翻转各组内部垂直堆叠顺序
# 散点图 + 折线图
series <- data.frame(time = c(rep(1, 4), rep(2, 4), rep(3, 4), rep(4, 4)), type = rep(c("a",
"b", "c", "d"), 4), value = rpois(16, 10))
ggplot(series, aes(time, value, group = type)) + geom_line(aes(colour = type)) +
geom_point(aes(colour = type)) # 默认identity
ggplot(series, aes(time, value, group = type)) + geom_line(aes(colour = type),
position = "stack") + geom_point(aes(colour = type), position = "stack") # stack堆积
ggplot(series, aes(time, value, group = type)) + geom_line(aes(colour = type),
position = position_stack(vjust = 0.5)) + geom_point(aes(colour = type),
position = position_stack(vjust = 0.5)) # 向下移动半个单位,以最下面的元素为高度为基准
ggplot(series, aes(time, value, group = type)) + geom_line(aes(colour = type),
position = position_stack(vjust = 0)) + geom_point(aes(colour = type), position = position_stack(vjust = 0)) # 向下移动到底,最下面的折线都拉直了
library(ggplot2)
ggplot(mtcars, aes(am, vs)) +
geom_jitter()
ggplot(mtcars, aes(am, vs)) +
geom_jitter(width = 0.1, height = 0.1) # 增加水平抖动10%,垂直抖动10%
ggplot(mtcars, aes(am, vs)) +
geom_point(position = position_jitter(width = 0.1, height = 0.1)) # 原理与上面一样,但是抖动是随机的,每次结果都可能不一样
ggplot(mtcars, aes(am, vs)) +
geom_point(position = "jitter") + # 默认抖动50%
geom_point(aes(am + 0.2, vs + 0.2), position = "jitter", color = "red" ) # 可以在映射里面进行简单的运算
position_jitterdodge(jitter.width = NULL, jitter.height = 0, dodge.width = 0.75, seed = NA)
仅仅用于箱线图和点图在一起的情形,且有顺序的,必须箱子在前,点图在后,抖动只能用在散点几何对象中,
jitter.width 默认40%, jitter.height 默认0
library(ggplot2)
dsub <- diamonds[sample(nrow(diamonds), 1000), ]
ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) + geom_boxplot(outlier.size = 0) +
geom_point(shape = 23) # 23号点形状为菱形
ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) + geom_boxplot(outlier.size = 0) +
geom_point(shape = 23, position = position_jitterdodge(dodge.width = 0.1)) # 点分布于各组箱子10%宽度上,默认点水平抖动错开
ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) + geom_boxplot(outlier.size = 0) +
geom_point(shape = 23, position = position_jitterdodge(dodge.width = 0.8)) # 点分布于各组箱子80%宽度上,默认点水平抖动错开
library(ggplot2)
df <- data.frame(x = c(1, 3, 2, 5), y = c("a", "c", "d", "c"))
ggplot(df, aes(x, y)) + geom_point() + geom_text(aes(label = y)) # 文本对象位置与点重合,视觉效果不好
ggplot(df, aes(x, y)) + geom_point() + geom_text(aes(label = y), position = position_nudge(y = -0.1)) # 文本位置向下移动1%个单位,错开文本与点位置
————————————————
版权声明:本文为CSDN博主「R语言中文社区」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kMD8d5R/article/details/84312225
相比几何对象,增加了:
统计变换函数 | 描述 | 其它 |
---|---|---|
stat_bin | 直方图 | 分割数据,然后绘制直方图 |
stat_function | 函数曲线 | 增加函数曲线图 |
stat_qq | Q-Q图 | |
stat_smooth | 平滑曲线 | |
stat_ellipse | 椭圆 | 常用于椭圆形置信区间,带状置信区间用geom_ribbon |
stat_spoke | 绘制有方向的数据点 | |
stat_sum | 绘制不重复的取值之和 | |
stat_summary | 分组汇总 | 可以求每组的均值,中位数等 |
stat_unique | 绘制不同的数据,去掉重复值 | |
stat_ecdf | 经验累计密度图 | |
stat_xsline | 样条曲线拟合 | 见基础运算_3 |
查询其它的统计变换函数:
ggplot2 parts of the tidyverse
使用ls(pattern = ‘^stat_’, env = as.environment(‘package:ggplot2’))
library(ggplot2)
ls(pattern = "^stat_", env = as.environment("package:ggplot2"))
要求数据源的y能够被分组,每组不止一个元素, 或增加一个分组映射,即aes(x= , y = , group = )
stat_summary (mapping = NULL, data = NULL, geom = "pointrange", position = "identity",
..., fun.data = NULL, fun.y = NULL, fun.ymax = NULL, fun.ymin = NULL,
fun.args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
参数解释:
library(ggplot2)
library(Hmisc)
g <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
g + stat_summary(fun.data = "mean_cl_boot", color = "red", size = 2) # 用mean_cl_bool对mpg进行运算,返回均值,最大值,最小值3个向量组成的矩阵
g + stat_summary(fun.y = "median", color = "red", size = 2, geom = "point") # 计算各组中位数
g + stat_summary(fun.y = "mean", color = "red", size = 2, geom = "point") # 计算各组均值
g + aes(color = factor(vs)) + stat_summary(fun.y = mean, geom = "line") # 增加1组颜色变量映射,然后求均值并连线
g + stat_summary(fun.y = mean, fun.ymin = min, fun.ymax = max, color = "red") # 计算各组均值,最值
# stat_summary_bin
g1 <- ggplot(diamonds, aes(cut))
g1 + geom_bar() # 条形图 ,只有1个映射的时候默认为计数
g1 + stat_summary_bin(aes(y = price), fun.y = "mean", geom = "bar") # 分组计算均值
# stat_sum_df用矩形将最值与均值框起来
stat_sum_df <- function(fun, geom = "crossbar", ...) {
stat_summary(fun.data = fun, color = "red", geom = geom, width = 0.2, ...)
}
g2 <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
g2 + stat_sum_df("mean_cl_boot", mapping = aes(group = cyl)) # 增加1个分组映射
g2 + stat_sum_df("mean_sdl", mapping = aes(group = cyl))
g2 + stat_sum_df("mean_sdl", fun.args = list(mult = 1), mapping = aes(group = cyl))
g2 + stat_sum_df("median_hilow", mapping = aes(group = cyl))
需要2个映射变量aes(group = , y = )
stat_function(mapping = NULL, data = NULL, geom = "path", position = "identity",
..., fun, xlim = NULL, n = 101, args = list(), na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE)
参数解释:
library(ggplot2)
set.seed(1492)
df <- data.frame(
x = rnorm(100)
)
x <- df$x
base <- ggplot(df, aes(x)) + geom_density() # 核密度图,展示变量分布规律,与频率分布直方图原理相同
base + stat_function(fun = dnorm, color = "red") # dnorm表示正态分布密度函数
base + stat_function(fun = dnorm, colour = "red", args = list(mean = 3)) # args传参给fun,生成均值为3的正态分布密度图
ggplot(data.frame(x = c(0, 2)), aes(x)) +
stat_function(fun = exp, geom = "line") # 画e^x在(0, 2)区间的函数图形,数据点由插值产生
ggplot(data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = dnorm) # 画在区间(-5, 5)区间的正态分布密度图,数据点由插值产生
ggplot(data.frame(x = c(-5, 5)), aes(x)) +
stat_function(fun = dnorm, args = list(mean = 2, sd = .5)) # 画均值为2,标准差为0.5的正态分布密度图
f <- ggplot(data.frame(x = c(0, 10)), aes(x))
f + stat_function(fun = sin, color = "red") + # 绘制(0, 10)区间的正弦函数图形
stat_function(fun = cos, color = "blue") # 绘制(0, 10)区间的余弦函数图形
myfunction <- function(x) {x^2 + x + 20}
f + stat_function(fun = myfunction) # 画自定义函数图像
fun1 <- function(x) {0.5 * x}
fun2 <- function(x) {x / (x +1)}
fun3 <- function(x) {0.5 * x - x*(x + 1)}
ggplot(data.frame(x = -5:5), aes(x)) + stat_function(fun = fun1, color = "red") +
stat_function(fun = fun2, color = "blue") +
stat_function(fun = fun3, color = "yellow", size = 4)
stat_smooth (mapping = NULL, data = NULL, geom = "smooth", position = "identity",
..., method = "auto", formula = y ~ x, se = TRUE, n = 80,
span = 0.75, fullrange = FALSE, level = 0.95, method.args = list(),
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
参数解释:
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth() + stat_smooth(method = lm,
se = FALSE) # 不显示置信区间
ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth(method = lm, formula = y ~
splines::bs(x, 3), se = FALSE)
ggplot(mpg, aes(displ, hwy, color = class)) + geom_point() + geom_smooth(se = FALSE,
method = lm)
ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth(span = 0.8) + geom_smooth(method = loess,
formula = y ~ x) + facet_wrap(~drv)
ggplot2默认为cartesian笛卡尔坐标系,其它坐标系都是通过笛卡尔坐标系画图,然后变换过来的,坐标函数如下:
坐标变换函数 | 描述 |
---|---|
coord_cartesian() | 笛卡尔坐标系 |
coord_fixed() | 固定纵横比笛卡尔坐标系 |
coord_flip() | 翻转坐标系 |
coord_polar() | 极坐标投影坐标系 |
coord_map(), coord_quickmap() | 地图投影(球面投影) |
coord_trans() | 变比例笛卡尔坐标系 |
注: 默认为笛卡尔坐标系,以下参数几乎用不上,可略过
coord_cartesian(xlim = NULL, ylim = NULL, expand = TRUE, default = FALSE, clip = “on”)
参数解释:
coord_cartesian()为纵横比没有固定的坐标系,表示纵轴和横轴的相对单位长度没有固定,
增加数据,则原图形的比例会变,背景都是正方形格子
而coord_fixed()坐标系纵横比可以设置固定,纵横比可以用参数ratio自定义,背景为矩形格子,
固定纵横比后,无论什么图形,其比例都是一样的,常用于横轴,纵轴都是数字的情况
语法:
coord_fixed(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE, clip = "on")
参数ratio表示指定纵横比,默认为1表示固定纵横比为1, 纵横比越大,则同样尺寸,其纵轴视觉长度越长
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + coord_fixed(ratio = 1) # 固定纵横比为1
p + coord_fixed(ratio = 5) # 固定纵横比为5,变高变窄
p + coord_fixed(ratio = 1/5) # 纵横比小于1,变矮变宽
p + coord_fixed(xlim = c(15, 30)) # 默认纵横比为1,设定x轴显示范围为15到30
翻转坐标系指翻转笛卡尔坐标的横轴和纵轴位置,翻转后柱形图变成条形图
coord_flip(xlim = NULL, ylim = NULL, expand = TRUE, clip = "on")
内部参数与标准笛卡尔坐标系一样,不用介绍
翻转后横轴为y轴,纵轴为x轴
h <- ggplot(diamonds, aes(carat)) + geom_histogram()
h
h + coord_flip() # 翻转坐标系
能将笛卡尔坐标, coord_polar(theta = "x", start = 0, direction = 1, clip = "on")
参数解释:
rm(list = ls())
gc() # 清空内存
library(ggplot2)
pie <- ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) + geom_bar(width = 1)
pie
pie + coord_polar(theta = "x") # x轴极化, x刻度值都一样,所以变成多层圆环,y轴刻度值对应圆环半径
pie + coord_polar(theta = "y") # y轴极化, y轴刻度值对应扇形弧度,x轴长度对应扇形半径
pie + coord_polar(theta = "y", start = pi/6, direction = 1) # 起始位置为距离12点针方向30度,顺时针排列
pie + coord_polar(theta = "y", start = pi/6, direction = -1) # 逆时针排列,起始位置与上面不一样
pie + coord_polar(theta = "y", start = -pi/6, direction = 1) # 起始位置与上面一样,但排列顺序不一样
rm(list = ls())
gc() # 清空内存
library(ggplot2)
set.seed(42)
small <- diamonds[sample(nrow(diamonds), 1000), ]
ggplot(data = small) + geom_bar(aes(x = clarity, fill = cut)) + coord_polar() +
scale_fill_brewer(type = "qual", palette = "Set2", direction = -1)
ggplot2极坐标转化不能制作雷达图,可以用ggradar包,安装方法devtools::install_github(“ricardo-bion/ggradar”)
ggradar支持的数据形式与ggplot2有些区别,采用行分类,宽数据最好,好在雷达图的数据量都比较小
ggradar智能化程度非常高,导入适合的数据就能出图,后期美化可以慢慢来
rm(list = ls())
gc() # 清空内存
library(ggradar)
mydata <- matrix(runif(40, 0, 1), 5, 8) # 构造数据集,5行8列的矩阵
rownames(mydata) <- LETTERS[1:5] # 大写字母为矩阵行命名
colnames(mydata) <- c("Apple", "Google", "Facebook", "Amozon", "Tencent", "Alibaba",
"Baidu", "Twitter") # 矩阵列命名
mynewdata <- data.frame(mydata) # 将矩阵转化为数据框
Name <- c("USA", "CHN", "UK", "RUS", "JP")
mynewdata <- data.frame(Name, mynewdata) # 增加一列字符串数据
mynewdata
# 单序列:
ggradar(mynewdata[2, ]) # 以列名为变量,对第2行数据进行绘图,显示各个公司在中国的业务
# 多序列:
ggradar(mynewdata) # 对所有行同时作图
原始的笛卡尔坐标上,坐标轴上的刻度比例尺是不变的,而coord_trans轴上刻度比例尺是变化的,
这种坐标系应用很少,但不是没用, 可以将曲线变成直线显示, 如果数据点在某个轴方向的密集程度是变化的,这样不便于观察,可以通过改变比例尺来调节,使数据点集中显示,更加方便观察
语法: coord_trans(x = "identity", y = "identity", limx = NULL, limy = NULL, clip = "on", xtrans, ytrans)
参数解释:
library(ggplot2)
ggplot(diamonds, aes(log10(carat), log10(price))) +
geom_point() # 正常笛卡尔坐标系
# 通过设置坐标轴标度,使坐标轴比例尺渐变
ggplot(diamonds, aes(carat, price)) +
geom_point() +
scale_x_log10() + # 坐标轴刻度对数变换
scale_y_log10()
# 采用变换笛卡尔坐标轴,结果与上面一样
ggplot(diamonds, aes(carat, price)) +
geom_point() +
coord_trans(x = "log10", y = "log10")
# 线性拟合
d <- subset(diamonds, carat > 0.5)
ggplot(d, aes(carat, price)) +
geom_point() +
geom_smooth(method = "lm") +
coord_trans(x = "log10", y = "log10") # lm线性拟合结果为直线,但变换坐标轴后变成了曲线
ggplot(d, aes(carat, price)) +
geom_point() +
geom_smooth(method = "lm") +
scale_x_log10() +
scale_y_log10() # 通过调整标度的方式,仍然为直线,点的位置并没有发生改变
df <- data.frame(a = abs(rnorm(26)),letters)
plot <- ggplot(df,aes(a,letters)) + geom_point()
plot + coord_trans(x = "log10") # 对x坐标轴比例尺对数运算
plot + coord_trans(x = "sqrt") # 对x轴坐标轴比例尺开方运算
地图投影需要特殊的数据源和很多扩展包,会在其它章节单独演示
标度用于控制变量映射到视觉对象的具体细节,如坐标轴标签和图例
视觉对象分为:
坐标轴,alpha透明度,color/fill颜色,date/time时间轴, hue色相, grey灰度,
shape点形, size尺寸, linetype线型, radius半径, area面积
它们都有相应的标度函数
分为简单函数和复合函数,复合函数内包含简单函数,简单函数如下:
复合标度函数:
除去默认的8个系统默认的标度scale_xxx_identity(),软件默认的一般用不上,用得上就不需要改了, 这8个分别是:
scale_color_identity(), scale_fill_identity(), scale_shape_identity(), scale_linetype_identity(),
scale_alpha_identity(), scale_size_identity(), scale_discrete_identity(), scale_continuous_identity()
还有一个手动处理任意离散变量的标度函数scale_discrete_manual(),其增加了1个映射参数,如aesthetics = c(“color”, “fill”)
现在还剩下11组标度函数,分类如下:
还支持scales包内的其它变换函数,如scales::percent()百分比刻度,自定义scales::trans_new()
library(ggplot2)
p1 <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
p1
p1 +
scale_x_continuous(name = "发动机排量/L", limits = c(2,6), breaks = c(2, 4, 6), labels = c("two", "four", "six"),
minor_breaks = c(3, 5)) + # 重新指定坐标轴名称, 只显示区间(2, 6)范围的元素, 更改坐标轴刻度和标签
scale_y_continuous(name = "高速公路油耗" ) # 重新指定坐标轴名称
set.seed(14)
df <- data.frame(
x = rnorm(10) * 100000,
y = seq(0, 1, length.out = 10)
)
p2 <- ggplot(df, aes(x, y)) + geom_point(shape=21, color = "purple", fill = "cyan", size = 5)
p2 + scale_y_continuous(labels = scales::percent) # y轴百分号显示
p2 + scale_x_continuous(labels = scales::comma) + # 数字逗号分割,每3位数增加1个逗号
scale_y_continuous(labels = scales::dollar) # y轴美元单位显示
scale_xxx_gradient^()才支持trans内置参数
library(ggplot2)
# scale_fill_continuous
v <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) + geom_tile()
v
v + scale_fill_continuous(type = "gradient", name = "密度", breaks = c(0, 0.05,
0.01, 0.02, 0.03, 0.04), labels = c(0, 0.05, 0.01, 0.02, 0.03, 0.04), position = "left") # 更改图例名称,不支持刻度点,图例标签位置,匹配方向等参数
v + scale_fill_continuous(type = "viridis") # 结果与scale_fill_viridis_c()一样
# scale_color_distiller
set.seed(19)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
(d <- ggplot(dsamp, aes(carat, price)) + geom_point(aes(colour = clarity)))
v <- ggplot(faithfuld) + geom_tile(aes(waiting, eruptions, fill = density))
v
v + scale_fill_distiller(palette = "Spectral", breaks = c(0, 0.05, 0.01, 0.02,
0.03, 0.04), labels = c(0, 0.05, 0.01, 0.02, 0.03, 0.04)) # 更改图例刻度及标签失败
# scale_xxx_gradient^()
set.seed(18)
df <- data.frame(x = runif(100), y = runif(100), z1 = rnorm(100), z2 = abs(rnorm(100)))
ggplot(df, aes(x, y)) + geom_point(aes(colour = z2)) # 默认从light blue到dark blue
ggplot(df, aes(x, y)) + geom_point(aes(colour = z2)) + scale_colour_gradient(low = "white",
high = "black", name = "这是图例名称", breaks = c(0, 0.5, 1, 2, 3, 4), labels = c(0,
0.5, 1, 2, 3, 4)) # 灰度渐变,修改图例名称,图例刻度及刻度标签
ggplot(df, aes(x, y)) + geom_point(aes(colour = z1), size = 2) + scale_colour_gradient2() # 两极渐变,
ggplot(df, aes(x, y)) + geom_point(aes(fill = z2), shape = 21, size = 3) + scale_fill_gradient2(low = "blue",
mid = "red", midpoint = 2, high = "cyan", name = "这是图例名称") # 修改基础颜色,
ggplot(df, aes(x, y)) + geom_point(aes(colour = z1)) + scale_colour_gradientn(colours = terrain.colors(10),
name = "这是图例名称") # 应用terrain.colors内置色板,更改标度方向失败
两极渐变:
修改基础颜色:
应用terrain.colors内置色板,更改标度方向失败:
library(ggplot2)
# scale_color_brewer
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
(d <- ggplot(dsamp, aes(carat, price)) + geom_point(aes(colour = clarity)))
d + scale_colour_brewer(name = "钻石透明度", palette = "Greens", direction = -1,
position = "left") # 更改图例名称,色板,标度方向,更改位置失败
d + scale_colour_brewer(palette = "Set1", name = "钻石透明度") # 多种颜色也可以渐变,非常方便
# scale_color_manual
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(colour = factor(cyl)))
p + scale_colour_manual(values = c("red", "blue", "green"))
# 按名称属性匹配
cols <- c(`8` = "red", `4` = "blue", `6` = "darkgreen", `10` = "orange") # 设定名称属性
p + scale_colour_manual(values = cols) # 名称属性匹配
p + scale_colour_manual(name = "这是图例名字", values = cols, breaks = c("4",
"6", "8"), labels = c("four", "six", "eight"), limits = c("4", "6", "8",
"10"))
library(ggplot2)
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(alpha = year))
p
p + scale_alpha(range = c(0.4, 0.8), name = "这是图例名称") # 设定透明度范围为0.4到0.8
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
d <- ggplot(dsamp, aes(carat, price)) + geom_point(aes(colour = clarity))
d + scale_colour_hue(name = "这是图例名称") # 默认全色轮
d + scale_colour_hue(l = 70, c = 150, name = "这是图例名称")# 调整色度和亮度
d + scale_colour_hue(h = c(0, 90), "这是图例名称") # 设定色相范围,0代表红色与橙色的分界
d + scale_colour_hue(h = c(90, 180), "这是图例名称")
d + scale_colour_hue(h = c(270, 360), "这是图例名称")
# 灰度
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(colour = factor(cyl)))
p + scale_colour_grey()
p + scale_colour_grey(start = 0.8, end = 0.2, name = "这是图例名称") # 修改灰度范围
# 灰度增加缺失值映射
miss <- factor(sample(c(NA, 1:5), nrow(mtcars), replace = TRUE))
ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(colour = miss)) + # 若缺失向量是data数据的子集,则可以映射
scale_colour_grey(na.value = "green", name = "这是图例名称") # 增加缺失值为绿色
library(ggplot2)
# 离散变量
txsamp <- subset(txhousing, city %in% c("Houston", "Fort Worth", "San Antonio",
"Dallas", "Austin"))
d <- ggplot(data = txsamp, aes(x = sales, y = median)) + geom_point(aes(colour = city))
d
d + scale_colour_viridis_d() # 默认viridis色板
d + scale_colour_viridis_d(option = "plasma", name = "这是图例名称") # 更改色板为plasma
d + scale_colour_viridis_d(option = "B", name = "这是图例名称") # 更改色板为inferno
d + scale_colour_viridis_d(option = "magma", name = "这是图例名称")
d + scale_colour_viridis_d(option = "cividis", name = "这是图例名称")
# 连续变量
v <- ggplot(faithfuld) + geom_tile(aes(waiting, eruptions, fill = density))
v + scale_fill_viridis_c()
v + scale_fill_viridis_c(option = "plasma", name = "这是图例名称")
set.seed(15)
dsmall <- diamonds[sample(nrow(diamonds), 100), ]
d <- ggplot(dsmall, aes(carat, price)) + geom_point(aes(shape = cut), color = "blue",
position = position_jitter(width = 0.1, height = 0.3))
d
d + scale_shape(name = "钻石切割水平", breaks = c("Fair", "Good", "Very Good",
"Premium", "Ideal"), labels = c("一般", "还行", "很好", "上好", "顶级")) # 设置图例的名称和刻度标签
oneshape <- c(25:21)
levels(dsmall$cut) <- c("Fair", "Good", "Very Good", "Premium", "Ideal") # 给要映射的变量增加因子水平,然后通过因子水平匹配
dsmall <- diamonds[sample(nrow(diamonds), 100), ]
d <- ggplot(dsmall, aes(carat, price)) + geom_point(aes(shape = cut), color = "blue",
position = position_jitter(width = 0.1, height = 0.3)) #
d + scale_shape_manual(values = oneshape, name = "切割工艺") # values的顺序与变量的因子水平一致
p <- ggplot(mpg, aes(displ, hwy, size = hwy)) + geom_point(position = position_jitter(width = 0.1,
height = 0.2), shape = 21, fill = "green")
p
p + scale_radius(name = "高速公路油耗", range = c(2, 10), limits = c(0, 30)) # 指定半径从2变化到10,即最小半径为2,最大半径为10,只显示值小于30的数据点
p + scale_size(name = "高速公路油耗", range = c(2, 10), limits = c(0, 30), breaks = c(2,
12, 25, 30), labels = c("2L", "12L", "25L", "30L")) # 设置图例刻度及标签
p + scale_size_area(name = "高速公路油耗", max_size = 8, breaks = c(2, 12, 25,
30), labels = c("2L", "12L", "25L", "30L")) # 指定最大面积为8,最小面积从0开始
例:
library(ggplot2)
last_month <- Sys.Date() - 0:29
set.seed(16)
df <- data.frame(date = last_month, price = runif(30))
base <- ggplot(df, aes(date, price)) + geom_line()
base + scale_x_date(name = "日期", date_labels = "%b %d") # 格式化参数,%b表示月份英文缩小,%d表示月份中第几天
base + scale_x_date(name = "日期", date_breaks = "1 week", date_labels = "第%W周",
date_minor_breaks = "1 day", limits = c(Sys.Date() - 7, NA)) # 时间跨度为1周,%W表示年内的星期数,第几周, 设定细网格时间跨度为1天, 设定显示范围为7天前至今
分面有2种函数:
library(ggplot2)
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()
p + facet_wrap(vars(class)) # 封装分面,使用class变量作为分面维度
p + facet_wrap(~class) # 与上面一样
p + facet_wrap(vars(class), nrow = 4) # 重新排列分面,分4行排
p + facet_wrap(vars(cyl, drv)) # 高维分面,按cyl和drv两个变量分面
p + facet_wrap(c("cyl", "drv"), labeller = "label_both") # 更改分面标题内容,显示分面变量名和变量值
p + facet_wrap(~class, scales = "free") # 更改facets方式,设定x轴自适应
p + facet_wrap(~class, scales = "free_y", nrow = 2, strip.position = "bottom") # 设定y轴自适应,2行排列,分面标题置于底部
library(ggplot2)
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p + facet_grid(rows = vars(drv)) # 以drv变量行分面
p + facet_grid(cols = vars(cyl)) # 以cyl变量列分面
p + facet_grid(vars(drv), vars(cyl)) # 同时行分面和列分面
p + facet_grid(. ~ cyl) # 更改facets方式,以cyl列分面
p + facet_grid(drv ~ .) # 以drv变量行分面
p + facet_grid(drv ~ cyl) # 以drv变量行分,以cyl变量列分
mg <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
mg + facet_grid(vs + am ~ gear, scales = "free_y") # 高维分面,y轴自适应
library(ggplot2)
# label_parsed解析
mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "gamma")) # 新增一个特殊字符向量
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p + facet_grid(. ~ cyl2, labeller = label_parsed) # label_parsed解析
# label_bquote数学公式
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p + facet_grid(. ~ vs, labeller = label_bquote(cols = .(vs)^.(vs))) # 变量指数形式显示
图例函数:
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")) # 更改图例方向
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) # 图例主题中修改刻度标签角度
)
)
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 = factor(1:5), q = 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)
)
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))
所有的新增坐标轴都是基于现有坐标轴变换而来的
参数解释:
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
内部函数及参数非常多,跟present data没有一点关系,新手不建议学,会搅乱思路
针对新手,建议使用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弹窗中进行操作
主题模板包,包括ggthemes, ggtech, ggthemer, ggsci,
有很多种风格:
对于著名科技公司,如谷歌,twiter等
著名可视化软件,如D3,Tableau等, 还有著名学术期刊,如SCI,柳叶刀等
还有著名出版物,如华尔街日报等
总之,应有尽有
在套用主题模板前,先看几个主题修改函数:
使用希腊字符、上标、下标及数学公式,都需要利用一个函数:expression(),具体使用方式如下:
plot(cars)
title(main = expression(Sigma))
expression()中的下标为[],上标为^,空格为~,连接符为*。示例代码如下:
plot(cars)
title(main = expression(Sigma[1]~'a'*'n'*'d'~Sigma^2))
想达到上面的效果,我们其实可以使用paste()与expression()进行组合,不需要上述繁琐的过程,也能够达到我们上述一模一样的输出,并且方便快捷:
plot(cars)
title(main = expression(paste(Sigma[1], ' and ', Sigma^2)))
目标:
代码:
expression(paste((frac(1, m)+frac(1, n))^-1, ABCD[paste(m, ',', n)]))
最后的数学公式,只需要在expression()中进行相应的符号连接即可,具体要求可参考:Mathematical Annotation in R,这里不再进行赘述。
geom_label {ggplot2} R Documentation
Text
geom_text() adds text directly to the plot. geom_label() draws a rectangle behind the text, making it easier to read.
geom_label(mapping = NULL, data = NULL, stat = "identity",
position = "identity", ..., parse = FALSE, nudge_x = 0,
nudge_y = 0, label.padding = unit(0.25, "lines"),
label.r = unit(0.15, "lines"), label.size = 0.25, na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE)
geom_text(mapping = NULL, data = NULL, stat = "identity",
position = "identity", ..., parse = FALSE, nudge_x = 0,
nudge_y = 0, check_overlap = FALSE, na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE)
Note that the “width” and “height” of a text element are 0, so stacking and dodging text will not work by default, and axis limits are not automatically expanded to include all text. Obviously, labels do have height and width, but they are physical units, not data units. The amount of space they occupy on the plot is not constant in data units: when you resize a plot, labels stay the same size, but the size of the axes changes.
geom_text() and geom_label() add labels for each row in the data, even if coordinates x, y are set to single values in the call to geom_label() or geom_text(). To add labels at specified points use annotate() with annotate(geom = “text”, …) or annotate(geom = “label”, …).
geom_text() understands the following aesthetics (required aesthetics are in bold):
x
y
label
alpha
angle
colour
family
fontface
group
hjust
lineheight
size
vjust
Learn more about setting these aesthetics in vignette(“ggplot2-specs”).
Currently geom_label() does not support the angle aesthetic and is considerably slower than geom_text(). The fill aesthetic controls the background colour of the label.
You can modify text alignment with the vjust and hjust aesthetics. These can either be a number between 0 (right/bottom) and 1 (top/left) or a character (“left”, “middle”, “right”, “bottom”, “center”, “top”). There are two special alignments: “inward” and “outward”. Inward always aligns text towards the center, and outward aligns it away from the center.
p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
p + geom_text()
# Avoid overlaps
p + geom_text(check_overlap = TRUE)
# Labels with background
p + geom_label()
# Change size of the label
p + geom_text(size = 10)
# Set aesthetics to fixed value
p + geom_point() + geom_text(hjust = 0, nudge_x = 0.05)
p + geom_point() + geom_text(vjust = 0, nudge_y = 0.5)
p + geom_point() + geom_text(angle = 45)
## Not run:
# Doesn't work on all systems
p + geom_text(family = "Times New Roman")
## End(Not run)
# Add aesthetic mappings
p + geom_text(aes(colour = factor(cyl)))
p + geom_text(aes(colour = factor(cyl))) +
scale_colour_discrete(l = 40)
p + geom_label(aes(fill = factor(cyl)), colour = "white", fontface = "bold")
p + geom_text(aes(size = wt))
# Scale height of text, rather than sqrt(height)
p + geom_text(aes(size = wt)) + scale_radius(range = c(3,6))
# You can display expressions by setting parse = TRUE. The
# details of the display are described in ?plotmath, but note that
# geom_text uses strings, not expressions.
p + geom_text(aes(label = paste(wt, "^(", cyl, ")", sep = "")),
parse = TRUE)
# Add a text annotation
p +
geom_text() +
annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
# Aligning labels and bars --------------------------------------------------
df <- data.frame(
x = factor(c(1, 1, 2, 2)),
y = c(1, 3, 2, 1),
grp = c("a", "b", "a", "b")
)
# ggplot2 doesn't know you want to give the labels the same virtual width
# as the bars:
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp), position = "dodge") +
geom_text(aes(label = y), position = "dodge")
# So tell it:
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp), position = "dodge") +
geom_text(aes(label = y), position = position_dodge(0.9))
# Use you can't nudge and dodge text, so instead adjust the y position
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp), position = "dodge") +
geom_text(
aes(label = y, y = y + 0.05),
position = position_dodge(0.9),
vjust = 0
)
# To place text in the middle of each bar in a stacked barplot, you
# need to set the vjust parameter of position_stack()
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp)) +
geom_text(aes(label = y), position = position_stack(vjust = 0.5))
# Justification -------------------------------------------------------------
df <- data.frame(
x = c(1, 1, 2, 2, 1.5),
y = c(1, 2, 1, 2, 1.5),
text = c("bottom-left", "bottom-right", "top-left", "top-right", "center")
)
ggplot(df, aes(x, y)) +
geom_text(aes(label = text))
ggplot(df, aes(x, y)) +
geom_text(aes(label = text), vjust = "inward", hjust = "inward")
Create an annotation layer
This function adds geoms to a plot, but unlike typical a geom function, the properties of the geoms are not mapped from variables of a data frame, but are instead passed in as vectors. This is useful for adding small annotations (such as text labels) or if you have your data in vectors, and for some reason don’t want to put them in a data frame.
annotate(geom, x = NULL, y = NULL, xmin = NULL, xmax = NULL,
ymin = NULL, ymax = NULL, xend = NULL, yend = NULL, ...,
na.rm = FALSE)
Note that all position aesthetics are scaled (i.e. they will expand the limits of the plot so they are visible), but all other aesthetics are set. This means that layers created with this function will never affect the legend.
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + annotate("text", x = 4, y = 25, label = "Some text")
p + annotate("text", x = 2:5, y = 25, label = "Some text")
p + annotate("rect", xmin = 3, xmax = 4.2, ymin = 12, ymax = 21,
alpha = .2)
p + annotate("segment", x = 2.5, xend = 4, y = 15, yend = 25,
colour = "blue")
p + annotate("pointrange", x = 3.5, y = 20, ymin = 12, ymax = 28,
colour = "red", size = 1.5)
p + annotate("text", x = 2:3, y = 20:21, label = c("my label", "label 2"))
p + annotate("text", x = 4, y = 25, label = "italic(R) ^ 2 == 0.75",
parse = TRUE)
p + annotate("text", x = 4, y = 25,
label = "paste(italic(R) ^ 2, \" = .75\")", parse = TRUE)
library(ggplot2)
head(economics)
head(economics_long)
## geom_line() 擅长时间序列作图
# geom_line() is suitable for time series
ggplot(economics, aes(date, unemploy)) + geom_line()
ggplot(economics_long, aes(date, value01, colour = variable)) +
geom_line()
# geom_step() is useful when you want to highlight exactly when
# the y value changes
recent <- economics[economics$date > as.Date("2013-01-01"), ]
ggplot(recent, aes(date, unemploy)) + geom_line()
ggplot(recent, aes(date, unemploy)) + geom_step()
# geom_path lets you explore how two variables are related over time,
# e.g. unemployment and personal savings rate
m <- ggplot(economics, aes(unemploy/pop, psavert))
m + geom_path()
m + geom_path(aes(colour = as.numeric(date)))
# Changing parameters ----------------------------------------------
ggplot(economics, aes(date, unemploy)) +
geom_line(colour = "red")
# Use the arrow parameter to add an arrow to the line
# See ?arrow for more details
c <- ggplot(economics, aes(x = date, y = pop))
c + geom_line(arrow = arrow())
c + geom_line(
arrow = arrow(angle = 15, ends = "both", type = "closed")
)
# Control line join parameters
df <- data.frame(x = 1:3, y = c(4, 1, 9))
base <- ggplot(df, aes(x, y))
base + geom_path(size = 10)
base + geom_path(size = 10, lineend = "round")
base + geom_path(size = 10, linejoin = "mitre", lineend = "butt")
# You can use NAs to break the line.
df <- data.frame(x = 1:5, y = c(1, 2, NA, 4, 5))
ggplot(df, aes(x, y)) + geom_point() + geom_line()
# Setting line type vs colour/size
# Line type needs to be applied to a line as a whole, so it can
# not be used with colour or size that vary across a line
x <- seq(0.01, .99, length.out = 100)
df <- data.frame(
x = rep(x, 2),
y = c(qlogis(x), 2 * qlogis(x)),
group = rep(c("a","b"),
each = 100)
)
p <- ggplot(df, aes(x=x, y=y, group=group))
# These work
p + geom_line(linetype = 2)
p + geom_line(aes(colour = group), linetype = 2)
p + geom_line(aes(colour = x))
# But this doesn't
should_stop(p + geom_line(aes(colour = x), linetype=2))