Rob Kabacoff
Saturday, April 23, 2022
Convert character values to dates
example
x <- c("10/2/2008", "12/5/2008")
xDate <- as.Date(x, format="m/d/Y")
library(ggplot2)
ggplot(data, aes(x = datevar, y = )) +
geom_line()
customize x-axis using
+ scale_x_date(date_breaks = , labels =)
ggplot(data, aes(x = datevar, y = )) +
geom_area(fill=, color=)
Stacked area chart
ggplot(data, aes(x = datevar, y = , fill=)) +
geom_area(color=)
ggplot2 graphs are static by nature
any ggplot2 graph can be made interactive via plotly
Steps
library(ggplot2)
library(plotly)
p <- ggplot(economics,
aes(x = date, y = psavert)) +
geom_line()
ggplotly(p)
Each observation should include longitude and latitude
library(leaflet)
leaflet() %>%
addProviderTiles(providers$namehere) %>%
setView(lng = , lat = , zoom = ) %>%
addCircles(lng =, lat =, popup =, color =)
type providers
at the console to see provider names
Easy choropleth maps with the choroplethr package by Ari Lamstein
Resulting maps are ggplot2 graphs and can be further modified.
library(chorplethr)
country_choropleth(data, "title", num_colors = , zoom = )
library(chorplethr)
state_choropleth(data, "title", num_colors = , zoom = )
library(chorplethr)
county_choropleth(data, "title", num_colors = ,
state_zoom = , county_zoom = )
modify choroplethr graph with ggplot2 functions
example
library(chorplethr)
county_choropleth(data, "title", num_colors = ,
state_zoom = , county_zoom = ) +
scale_fill_brewer(palette = "Set1")
see https://r-graph-gallery.com/38-rcolorbrewers-palettes.html