This is intended as a gallery / cookbook of different visualisation libraries in R. You can toggle overall code visibility in the top right-hand corner.
If you can improve the code or expand the charts or packages included, then please do so! Go to repo
The ggvis gallery is currently seperate as it has an old version of the vega library resulting in problems with the vegalite package right now.
ggplot2 is the way most people make plots with R these days.
source("genericprep.R")
if(!require(ggplot2)) install.packages("ggplot2")
library(ggplot2)
# Network chart
if(!require(GGally)) install.packages("GGally")
if(!require(network)) install.packages("network")
library(GGally)
# Beeswarm
if(!require(ggbeeswarm)) install.packages("ggbeeswarm")
library(ggbeeswarm)
ggplot(seatbelts, aes(x=date, y=DriversKilled)) +
geom_bar(stat="identity")
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,year]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled)) +
geom_bar(stat="identity")
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law=as.factor(law))]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled
, group=law, fill=law)) +
geom_bar(stat="identity")
ggplot(seatbelts, aes(x=PetrolPrice, y=DriversKilled)) +
geom_point()
ggplot(seatbelts, aes(x=PetrolPrice, y=DriversKilled)) +
geom_point(shape=1)
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,PetrolPrice=cut(PetrolPrice,breaks = 10))]
ggplot(seatbeltsAgg, aes(x=year, y=PetrolPrice
, size=DriversKilled)) +
geom_point()
ggplot(seatbelts, aes(x=PetrolPrice, y=DriversKilled
, colour = as.factor(law))) +
geom_point()
ggplot(seatbelts, aes(x=PetrolPrice, y=DriversKilled
, colour = as.factor(law)
, label = as.factor(law))) +
geom_text()
ggplot(seatbelts, aes(x=PetrolPrice, y=DriversKilled)) +
geom_point() +
scale_y_log10()
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,PetrolPrice)]
ggplot(seatbeltsAgg, aes(x=year, y=PetrolPrice
, size=DriversKilled)) +
geom_point()
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,year]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled)) +
geom_line()
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year, law=as.factor(law))]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled
, group=law , colour=law)) +
geom_line()
ggplot(seatbelts, aes(x=PetrolPrice, y=..count..) )+
geom_histogram()
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,year]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled)) +
geom_area()
ggplot(seatbelts, aes(x=cut(PetrolPrice,10), y=DriversKilled)) +
geom_boxplot()
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law=as.factor(law))]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled
,fill=law)) +
geom_bar(stat="identity")
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law=as.factor(law))]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled
,fill=law)) +
geom_bar(stat="identity") +
coord_flip()
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law=as.factor(law))]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled
,fill=law)) +
geom_bar(stat="identity",position = "fill")
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law=as.factor(law))]
dummyRow<-data.table(year=1984,law=0,DriversKilled=0)
seatbeltsAgg <- rbindlist(list(seatbeltsAgg,dummyRow), use.names=TRUE,fill=TRUE)
ggplot(seatbeltsAgg[order(year,law),], aes(x=year, y=DriversKilled
,fill=law)) +
geom_area(position="stack")
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law=as.factor(law))]
dummyRow<-data.table(year=1984,law=0,DriversKilled=0)
seatbeltsAgg <- rbindlist(list(seatbeltsAgg,dummyRow), use.names=TRUE,fill=TRUE)
ggplot(seatbeltsAgg[order(year,law),], aes(x=year, y=DriversKilled
,fill=law)) +
geom_area(position="fill")
# This is inelegant!
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law=as.factor(law))]
ggplot( mapping = aes(x=year, y=DriversKilled
,fill=law)) +
geom_bar(data=seatbeltsAgg[law=="1",],stat="identity", alpha=.5)+
geom_bar(data=seatbeltsAgg[law=="0",],stat="identity", alpha=.5)
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law)]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled)) +
geom_bar(stat="identity")+
facet_wrap(~law)
seatbeltsAgg<-seatbelts[
,.(DriversKilled = sum(DriversKilled))
,.(year,law,PetrolPrice=cut(PetrolPrice,breaks = 5))]
ggplot(seatbeltsAgg, aes(x=year, y=DriversKilled, fill=PetrolPrice)) +
geom_bar(stat="identity")+
facet_wrap(~law)
ggplot(seatbelts, aes(x=PetrolPrice, y=DriversKilled)) +
geom_point()+
facet_wrap(~law)
ggplot(seatbelts, aes(x=PetrolPrice, y=..count..) )+
geom_histogram()+
facet_wrap(~law)
ggplot(seatbelts, aes(x=PetrolPrice, y=DriversKilled
, colour = as.factor(law))) +
geom_point()+
facet_wrap(~law)
GGally::ggnet2(graphNet)
ggplot(seatbelts, aes(x=as.factor(law), y=DriversKilled)) +
ggbeeswarm::geom_beeswarm()
Vega is a grammar of graphics implementation that takes JSON and converts the results to HTML5 or SVG. Vega-Lite provides layer over the top of Vega to reduce the burden of generating the JSON specifications required.
Bob Rudis developed an R binding of vegalite, currently available on github( hrbrmstr/vegalite ).
Much of the code used in this is taken from, or loosely based on, the code Bob wrote in his reproduction of the Vega gallery.
Limitations:
source("genericprep.R")
# Not on CRAN
if(!require(devtools)) install.packages("devtools")
if(!require(vegalite)) {
devtools::install_github("hrbrmstr/vegalite")
}
library(vegalite)
vegalite <- vegalite(viewport_height = "300px")
vegalite %>%
add_data(seatbelts) %>%
encode_x("date","ordinal") %>%
encode_y("DriversKilled", "quantitative") %>%
mark_bar()
vegalite %>%
add_data(seatbelts) %>%
encode_x("year","ordinal") %>%
encode_y("DriversKilled", "quantitative","sum") %>%
mark_bar()
vegalite %>%
add_data(seatbelts) %>%
encode_x("year","ordinal") %>%
encode_y("DriversKilled", "quantitative","sum") %>%
encode_color("law", "nominal") %>%
mark_bar()
vegalite %>%
add_data(seatbelts) %>%
encode_x("PetrolPrice","quantitative") %>%
encode_y("DriversKilled", "quantitative") %>%
mark_point()
vegalite %>%
add_data(seatbelts) %>%
encode_x("PetrolPrice","quantitative") %>%
encode_y("DriversKilled", "quantitative") %>%
mark_circle()
vegalite %>%
add_data(seatbelts) %>%
encode_x("year","ordinal") %>%
encode_y("PetrolPrice", "quantitative") %>%
encode_size("DriversKilled", "quantitative", aggregate="sum") %>%
bin_y(maxbins=10) %>%
mark_point()
vegalite %>%
add_data(seatbelts) %>%
encode_x("PetrolPrice","quantitative") %>%
encode_y("DriversKilled", "quantitative") %>%
encode_color("law", "nominal") %>%
mark_point()
vegalite %>%
add_data(seatbelts) %>%
encode_x("PetrolPrice","quantitative") %>%
encode_y("DriversKilled", "quantitative") %>%
encode_color("law", "nominal") %>%
encode_text("law", "nominal") %>%
mark_text()
vegalite %>%
add_data(seatbelts) %>%
encode_x("year","ordinal") %>%
encode_y("DriversKilled", "quantitative") %>%
scale_y_log() %>%
mark_point()
vegalite %>%
add_data(seatbelts) %>%
encode_x("year","ordinal") %>%
encode_y("PetrolPrice", "quantitative") %>%
encode_size("DriversKilled", "quantitative", aggregate="sum") %>%
mark_point()
vegalite %>%
add_data(seatbelts) %>%
encode_x("year","ordinal") %>%
encode_y("DriversKilled", "quantitative", "sum") %>%
mark_line()
vegalite %>%
add_data(seatbelts) %>%
encode_x("year","ordinal") %>%
encode_y("DriversKilled", "quantitative", "sum") %>%
encode_color("law", "nominal") %>%
mark_line()
vegalite %>%
add_data(seatbelts) %>%
encode_x("PetrolPrice","quantitative") %>%
encode_y("DriversKilled", "quantitative", "sum") %>%
bin_x(maxbins=10) %>%
mark_bar()
vegalite %>%
add_data(seatbelts) %>%
encode_x("year","ordinal") %>%
encode_y("DriversKilled", "quantitative", "sum") %>%
mark_area()
sessionInfo()
## R version 3.2.4 Revised (2016-03-16 r70338)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu precise (12.04.5 LTS)
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] vegalite_0.6.1 scales_0.4.0 network_1.13.0 ggbeeswarm_0.5.0
## [5] GGally_1.0.1 ggplot2_2.1.0 sna_2.3-2 data.table_1.9.6
## [9] rmarkdown_0.9.5
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.3 formatR_1.3 RColorBrewer_1.1-2
## [4] git2r_0.14.0 plyr_1.8.3 vipor_0.3.2
## [7] tools_3.2.4 digest_0.6.9 base64_1.1
## [10] jsonlite_0.9.19 evaluate_0.8.3 memoise_1.0.0
## [13] gtable_0.2.0 curl_0.9.6 yaml_2.1.13
## [16] beeswarm_0.2.1 withr_1.0.1 stringr_1.0.0
## [19] httr_1.1.0 knitr_1.12.3 htmlwidgets_0.6
## [22] devtools_1.10.0 webshot_0.3 grid_3.2.4
## [25] reshape_0.8.5 R6_2.1.2 clipr_0.2.0
## [28] magrittr_1.5 htmltools_0.3.5 colorspace_1.2-6
## [31] labeling_0.3 stringi_1.0-1 munsell_0.4.3
## [34] chron_2.3-47