ggplot(seals, aes(x = long, y = lat)) + geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat))
3.5 长方形
1 2 3
ggplot(seals, aes(x = long, y = lat)) + geom_rect(aes(xmin = long, ymin = lat, xmax = long + delta_long, ymax = lat + delta_lat))
4 一维连续数据(向量)绘图
4.1 一维连续数据
1
a <- ggplot(mpg, aes(hwy))
4.2 频度阴影图
1 2
a + geom_area(stat = "bin") ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
4.3 核密度图
1
a + geom_density(kernel = "gaussian")
4.4 点状频度图
1 2
a + geom_dotplot() ## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
4.5 频度折线图
1 2
a + geom_freqpoly() ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
4.6 单变量离散数据
1 2
b <- ggplot(mpg, aes(fl)) b + geom_bar()
5 二维连续变量绘图
5.1 x和y均为连续变量
5.1.1 空白图形,只生成坐标
1 2
f <- ggplot(mpg, aes(cty, hwy)) f + geom_blank()
5.1.2 带随机扩散的散点图
1
f + geom_jitter()
5.1.3 散点图
1
f + geom_point()
5.1.4 分位数回归线
1 2 3 4 5 6 7 8
f + geom_quantile() ## Loading required package: SparseM ## ## Attaching package: 'SparseM' ## The following object is masked from 'package:base': ## ## backsolve ## Smoothing formula not specified. Using: y ~ x
5.1.5 xy轴投影
1
f + geom_rug(sides = "bl")
5.1.6 线性模型拟合以及置信区间
1
f + geom_smooth(method = "lm")
5.1.7 Loess拟合趋势线以及置信区间
1 2
f + geom_smooth(method = "auto") ## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
5.2 x为离散变量,y为连续变量
5.2.1 柱状图
1 2
g <- ggplot(mpg, aes(class, hwy)) g + geom_bar(stat = "identity")
5.2.2 箱线图
1
g + geom_boxplot()
5.2.3 点图
1 2
g + geom_dotplot(binaxis = "y", stackdir = "center") ## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
5.2.4 小提琴图
1
g + geom_violin(scale = "area")
5.3 x为离散变量, y为离散变量
1 2
h <- ggplot(diamonds, aes(cut, color)) h + geom_jitter()
5.4 二维密度分布图
5.4.1 栅格图
1 2 3
data(iris) i <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) i + geom_bin2d(binwidth = c(5, 0.5))
f + geom_quantile(quantiles = c(0.25, 0.5, 0.75), formula = y ~ log(x), method = "rq")
7.4.5 Loess回归以及置信区间统计变换的参数设定
1 2
f + geom_smooth(method = "auto", formula = y ~ x, se = TRUE, n = 80, fullrange = FALSE, level = 0.95) ## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
7.4.6 绘制dnorm的概率密度分布
1
ggplot() + stat_function(aes(x = -3:3), fun = dnorm, n = 101, args = list(sd=0.5))
7.4.7 绘制散点图
1
f + stat_identity()
7.4.8 绘制qq图
1
ggplot() + stat_qq(aes(sample=1:100), distribution = qt, dparams = list(df=5))
n + scale_fill_grey( start = 0.2, end = 0.8, na.value = "red")
9.1.4.2 通过 scale_fill_gradient显示颜色过渡
1 2 3
o <- a + geom_dotplot(aes(fill = ..x..)) o + scale_fill_gradient( low = "red", high = "yellow") ## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
9.1.4.3 通过scale_fill_gradient2显示颜色过渡
1 2
o + scale_fill_gradient2( low = "red", high = "blue", mid = "white", midpoint = 25 ) ## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
9.1.4.4 选择颜色序列
1 2
o + scale_fill_gradientn(colours = terrain.colors(6) ) ## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
`theme_few()`:Few’s “Practical Rules for Using Color in Charts”
`theme_fivethirtyeight()`: Theme inspired by fivethirtyeight.com plots
`theme_foundation()`
`theme_gdocs()` : google文档风格
`theme_hc()`: Highcharts Theme
`theme_igray()`: 逆向灰色梯度风格
`theme_map()`: 简约地图风格 Clean theme for maps
`theme_pander()` : A ggplot theme originated from the pander package
`theme_par()`: Theme which uses the current ‘base’ graphics parameter values from par(). Not all par() parameters, are supported, and not all are relevant to ggplot2 themes.
`theme_solarized()` `theme_solarized_2()`: ggplot color themes based on the Solarized palette
`theme_solid()`: Theme with nothing other than a background color
Hadley Wickham (2010). A layered grammar of graphics. Journal of Computational and Graphical Statistics, 19(1), 3-28.
Wilkinson, L. (2012). The grammar of graphics. In Handbook of Computational Statistics (pp. 375-414). Springer, Berlin, Heidelberg.
Wilkinson, L. (2011). ggplot2: Elegant Graphics for Data Analysis by WICKHAM, H. Biometrics, 67(2), 678-679.
Hadley Wickham (2007). Reshaping Data with the reshape Package. Journal of Statistical Software, 21(12), 1-20. [http://www.jstatsoft.org/v21/i12/](http://www.jstatsoft.org/v21/i12/).
Hadley Wickham, Romain François, Lionel Henry and Kirill Müller (2019). dplyr: A Grammar of Data Manipulation. R package version 0.8.0.1. [https://CRAN.R-project.org/package=dplyr](https://cran.r-project.org/package=dplyr)