Fundamentals & Workflows
— Exercise Solutions —
geom_line()
and geom_path()
?geom_smooth()
and stat_smooth()
interchangeably?
geom_path()
connects the observations in the order in which they appear in the data.geom_line()
connects them in order of the variable on the x axis.
geom_smooth()
andstat_smooth()
are effectively aliases: they both use the same arguments. Usestat_smooth()
if you want to display the results with a non-standard geom.
layer(data, mapping, geom, stat, position)
g1 <-
ggplot(bikes, aes(x = date, y = count)) +
geom_line(aes(color = day_night)) +
scale_x_date(date_breaks = "4 months", date_labels = "%m/'%y") +
scale_y_continuous(labels = scales::label_comma()) +
scale_color_manual(values = c(day = "#FFA200", night = "#757BC7")) +
labs(x = NULL, y = "Rented bikes", color = NULL)
g1
g3 <- ggplot(bikes, aes(x = weather_type, y = count, fill = day_night)) +
stat_summary(
geom = "col", fun = "sum",
width = .7,
position = position_dodge2(
padding = .2, preserve = "single"
)
) +
scale_y_continuous(labels = scales::label_comma(scale = 1/10^6, suffix = "M")) +
scale_fill_manual(
values = c(day = "#FFA200", night = "#757bc7")
) +
labs(x = NULL, y = "Rented bikes", fill = NULL) +
theme(
panel.grid.major.x = element_blank()
)
g3
Cédric Scherer // posit::conf(2023)