使用 ggplot2 进行数据可视化入门
Rick Scavetta
Founder, Scavetta Academy
| 图形类型 | 可用 Geom |
|---|---|
| 散点图 | points, jitter, abline, smooth, count |
| 条形图 | histogram, bar, col, errorbar |
| 折线图 | line, path |
ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram()
`stat_bin()` using `bins = 30`.
Pick better value with `binwidth`.

ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram()
# Default bin width:
diff(range(iris$Sepal.Width))/30
[1] 0.08

ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram(binwidth = 0.1)
始终为数据设置有意义的箱宽。
柱间无空隙。

ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram(binwidth = 0.1,
center = 0.05)
始终为数据设置有意义的箱宽。
柱间无空隙。
X 轴刻度位于柱间。

ggplot(iris, aes(x = Sepal.Width,
fill = Species)) +
geom_histogram(binwidth = .1,
center = 0.05)

ggplot(iris, aes(x = Sepal.Width,
fill = Species)) +
geom_histogram(binwidth = .1,
center = 0.05,
position = "stack")

ggplot(iris, aes(x = Sepal.Width,
fill = Species)) +
geom_histogram(binwidth = .1,
center = 0.05,
position = "dodge")

ggplot(iris, aes(x = Sepal.Width,
fill = Species)) +
geom_histogram(binwidth = .1,
center = 0.05,
position = "fill")

使用 ggplot2 进行数据可视化入门