library(readr)
library(ggplot2)
library(dplyr)
library(jhur)
read_bike()
function from jhur
package. Assign it to bike
variable.Then, use the provided code to compute a data frame bike_agg
with aggregate summary of bike lanes: average length of lanes (lane_avg_length
) for each year (dateInstalled
).
bike <- read_bike()
## Rows: 1631 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): subType, name, block, type, project, route
## dbl (3): numLanes, length, dateInstalled
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
bike_agg <-
bike %>%
# filter data to keep only these observations for which year is non-0
filter(dateInstalled != 0) %>%
group_by(dateInstalled) %>%
summarise(lane_avg_length = mean(length))
bike_agg
## # A tibble: 8 × 2
## dateInstalled lane_avg_length
## <dbl> <dbl>
## 1 2006 1469.
## 2 2007 310.
## 3 2008 249.
## 4 2009 407.
## 5 2010 246.
## 6 2011 233.
## 7 2012 271.
## 8 2013 290.
ggplot2
package make plot of average length of lanes (lane_avg_length
; y-axis) for each year (dateInstalled
; x-axis). You can use lines layer (+ geom_line()
) or points layer (+ geom_point()
), or both!Assign the plot to variable my_plot
. Type my_plot
in the console to have it displayed
ggplot(bike_agg, aes(x = dateInstalled, y = lane_avg_length)) +
geom_line() +
geom_point()
my_plot <-
ggplot(bike_agg, aes(x = dateInstalled, y = lane_avg_length)) +
geom_line() +
geom_point()
my_plot
my_plot <-
my_plot +
labs(x = "Year of bike lane installation",
y = "Average bike lane length",
title = "Average bike lane length 2006-2013")
my_plot
scale_x_continuous()
function and its arguments to modify x-axis look. Use scale_y_continuous()
function and its arguments to modify the y-axis.my_plot <-
my_plot +
scale_x_continuous(breaks = c(2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013)) +
scale_y_continuous(limits = c(0, NA)) # force y-axis to start from 0, do not force upper bound
my_plot
my_plot
while adding a different “theme” to it.my_plot + theme_bw()
my_plot + theme_classic()
my_plot + theme_dark()
my_plot + theme_gray()
my_plot + theme_void()
bike_agg_2
with aggregate summary of bike lanes: number of lanes (lane_count
) – separately for each year (dateInstalled
) and for each lane type.bike_agg_2 <-
bike %>%
filter(dateInstalled != 0) %>%
group_by(dateInstalled, type) %>%
summarise(lane_count = n())
## `summarise()` has grouped output by 'dateInstalled'. You can override using the `.groups` argument.
bike_agg_2
## # A tibble: 22 × 3
## # Groups: dateInstalled [8]
## dateInstalled type lane_count
## <dbl> <chr> <int>
## 1 2006 BIKE LANE 2
## 2 2007 BIKE LANE 127
## 3 2007 SHARROW 95
## 4 2007 SIGNED ROUTE 146
## 5 2008 BIKE LANE 55
## 6 2008 SHARROW 148
## 7 2008 SIDEPATH 3
## 8 2009 BIKE LANE 46
## 9 2009 SHARED BUS BIKE 30
## 10 2009 SHARROW 10
## # … with 12 more rows
ggplot2
package to make a plot showing trajectories of number of lanes (lane_count
; y-axis) over year (dateInstalled
; x-axis), where each bike line type has a different line with a different color (hint: use color = type
in mapping).ggplot(bike_agg_2, aes(x = dateInstalled,
y = lane_count,
color = type)) +
geom_line() +
geom_point()
+ facet_wrap(~ type, ncol = 3)
) to have data for each bike line type in a separate plot panel.(You may see geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
warning as some bike lane types will have only 1 point plotted while trying to plot a line). Save the new plot as an object called facet_plot
.
facet_plot <- ggplot(bike_agg_2,
aes(x = dateInstalled,
y = lane_count,
color = type)) +
geom_line() +
geom_point() +
facet_wrap(~ type, ncol = 3)
facet_plot
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?
Bonus: Modify facet_plot
to remove the legend and change the names of the axis titles to be “Number of bike lanes” for the x axis and “Date bike lane was installed” for the y axis.
facet_plot <- facet_plot +
theme(legend.position = "none") +
labs(x = "Number of bike lanes",
y = "Date bike lane was installed")
facet_plot
## geom_path: Each group consists of only one observation. Do you need to adjust
## the group aesthetic?