上次讲到了一些基础知识,这次说一说ggplot2图层中的geoms,由于geoms内容太多,所以分6次说。
图层结合了数据,映射,几何(对象),统计(变换)和位置调整。通常,将使用geom_函数创建图层,如果需要,覆盖默认位置和stat。
1. geom_abline 、geom_hline 、geom_vline——参考线:水平,垂直和对角线
2. geom_bar 、geom_col 、stat_count——条形图
3. geom_bin2d 、stat_bin_2d ——2d bin计数的热图
4. geom_blank——空图
5. geom_boxplot 、stat_boxplot——盒式图
###1. geom_abline 、geom_hline 、geom_vline——参考线:水平,垂直和对角线
函数格式:
geom_abline(mapping = NULL, data = NULL, ..., slope, intercept,
na.rm = FALSE, show.legend = NA)
geom_hline(mapping = NULL, data = NULL, ..., yintercept, na.rm = FALSE,
show.legend = NA)
geom_vline(mapping = NULL, data = NULL, ..., xintercept, na.rm = FALSE,
show.legend = NA)
Argument
library(devtools)
#install_github("easyGgplot2", "kassambara")
library(easyGgplot2)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
ggplot2.multiplot(p + geom_vline(xintercept = 5),p + geom_vline(xintercept = 1:5),p + geom_hline(yintercept = 20), cols=3)
plot1 <- p + geom_abline() # Can't see it - outside the range of the data
plot2 <- p + geom_abline(intercept = 20)
coef(lm(mpg ~ wt, data = mtcars))
#> (Intercept) wt
#> 37.285126 -5.344472
plot3 <- p + geom_abline(intercept = 37, slope = -5)
plot4 <- p + geom_smooth(method = "lm