Introduction to Data Visualization with ggplot2
Rick Scavetta
Founder, Scavetta Academy
Plot type | Possible Geoms |
---|---|
Scatter plots | points, jitter, abline, smooth, count |
Bar plots | histogram, bar, col, errorbar |
Line plots | 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)
Always set a meaningful bin widths for your data.
No spaces between bars.
ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram(binwidth = 0.1,
center = 0.05)
Always set a meaningful bin widths for your data.
No spaces between bars.
X axis labels are between bars.
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")
Introduction to Data Visualization with ggplot2