In [ ]:
library('tidyverse')
gglpot always has this form:
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
In [ ]:
ggplot(data = mpg)
In [ ]:
str(mpg)
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = cyl, y = hwy))
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = drv, y = class))
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue", shape = 1)
In [ ]:
str(mpg)
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = displ))
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class, alpha = class), size = 2)
In [ ]:
# 4:
# For shapes that have a border (like 21), you can colour the inside and
# outside separately. Use the stroke aesthetic to modify the width of the
# border
ggplot(mtcars, aes(wt, mpg)) +
geom_point(shape = 21, colour = "black", fill = "white", size = 5, stroke = 5)
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = displ < 5))
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)
In [ ]:
# 1.
str(mpg)
# ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy)) +
# facet_grid(. ~ displ)
In [ ]:
# 3.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ .)
In [ ]:
# 5. because facet_wrap is for 1-d. facet_wrap is for 2-d.
# 6. We don't have much space in a row.
In [ ]:
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv, color = drv)) +
geom_point(mapping = aes(x = displ, y = hwy, shape = drv, color = drv))
In [ ]:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth()
In [ ]:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)
In [ ]:
# 6.1
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
In [ ]:
ggplot() +
geom_point(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_smooth(data = mpg, mapping = aes(x = displ, y = hwy))
In [ ]:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = drv)) +
geom_smooth(se = FALSE)
In [ ]:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = drv)) +
geom_smooth(mapping = aes(linetype = drv), se = FALSE)
In [ ]:
# 6.6
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(color = "white", size = 4) +
geom_point(mapping = aes(color = drv))
# geom_point(color = "white", size = 2) +
stat_count() instead of geom_bar(). We can do this because every geom has a default stat; and every stat has a default geom.
In [ ]:
summary(diamonds)
In [ ]:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut))
In [ ]:
demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
)
summary(demo)
In [ ]:
ggplot(data = demo) +
geom_bar(mapping = aes(x = cut, y = freq), stat = "identity")
In [ ]:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
In [ ]:
# 1. geom_pointrange is associated with stat_summary()
ggplot(data = diamonds) +
geom_pointrange(mapping = aes(x = cut, y = depth), stat = "summary",
fun.ymin = min, fun.ymax = max, fun.y = median)
In [ ]:
# 2. geom does geom_bar(stat = "identity")
ggplot(data = demo) +
geom_col(mapping = aes(x = cut, y = freq))
In [ ]:
# 3. See: http://ggplot2.tidyverse.org/reference/index.html#section-layer-geoms
In [ ]:
# 4. stat_smooth computed variables: y/y_min/y_max/se. method/formula/span.
In [ ]:
# 5. The problem with the following code is that it treats each combination
# of cut and color as one group, so the proportion is always 1 (100%)
# But our intention is to treat all data as one group
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = color, y = ..prop..))
position_ function.
In [ ]:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity))
In [ ]:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "identity", alpha = 0.2)
In [ ]:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, colour = clarity), position = "identity", alpha = 0.2, fill = NA)
In [ ]:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
In [ ]:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
In [ ]:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), position = "jitter")
A: overplotting. Solution is to use geom_jitter or geom_count
A: width and height
In [ ]:
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_point() +
geom_jitter()
In [ ]:
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_point() +
geom_count()
geom_boxplot()? Create a visualisation of the mpg dataset that demonstrates it.A: The default position is "dodge"
In [ ]:
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot(mapping = aes(color = drv))
In [ ]:
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar + coord_flip()
In [ ]:
bar + coord_polar()
In [ ]:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), width = 1) +
coord_polar()
A: Control labels of axis
A: coord_fixed() makes the aspect ratio to be 1
In [ ]:
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_point() +
geom_abline() +
coord_fixed()