Load data and Libraries
if (!("ggplot2" %in% installed.packages())) {
install.packages('ggplot2')
}
library('ggplot2')
## Warning: package 'ggplot2' was built under R version 4.4.1
if (!("tidyverse" %in% installed.packages())) {
install.packages('tidyverse')
}
library('tidyverse')
## Warning: package 'tibble' was built under R version 4.4.1
## Warning: package 'purrr' was built under R version 4.4.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.3.0
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
if (!("dplyr" %in% installed.packages())) {
install.packages('dplyr')
}
library('dplyr')
if (!("devtools" %in% installed.packages())) {
install.packages('devtools')
}
if (!("canvasXpress" %in% installed.packages())) {
devtools::install_github('neuhausi/canvasXpress')
}
#devtools::install_local("~/git/canvas/R/canvasXpress.tar.gz", build_manual = TRUE, upgrade = "always")
library('canvasXpress')
Creating a ggplot
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
g

canvasXpress(g, width = 672, height = 500)
Aesthetic mappings - Color
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
g

canvasXpress(g, width = 672, height = 500)
Size
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class))
g
## Warning: Using size for a discrete variable is not advised.

canvasXpress(g, width = 672, height = 500)
## Warning: Using size for a discrete variable is not advised.
Alpha
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
g
## Warning: Using alpha for a discrete variable is not advised.

canvasXpress(g, width = 672, height = 500)
## Warning: Using alpha for a discrete variable is not advised.
Shape
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, shape = class))
g
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 7 values. Consider specifying shapes manually if you need
## that many of them.
## Warning: Removed 62 rows containing missing values or values outside the scale range
## (`geom_point()`).

canvasXpress(g, width = 672, height = 500)
## Warning: The shape palette can deal with a maximum of 6 discrete values because more
## than 6 becomes difficult to discriminate
## ℹ you have requested 7 values. Consider specifying shapes manually if you need
## that many of them.
Facets - wrap
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ class, nrow = 2)
g

canvasXpress(g, width = 672, height = 500)
Facets - grid
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_grid(drv ~ cyl)
g

canvasXpress(g, width = 672, height = 500)
Geometric objects
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
g

canvasXpress(g, width = 672, height = 500)
Smooth
g = ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy))
g
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

canvasXpress(g, width = 672, height = 500)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Smooth - Linetype
g = ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
g
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

canvasXpress(g, width = 672, height = 500)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Smooth - Color
g = ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy, color = drv), show.legend = FALSE)
g
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

canvasXpress(g, width = 672, height = 500)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Smoth - Point Color
g = ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy))
g
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

canvasXpress(g, width = 672, height = 500)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Smooth - Color2
g = ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth()
g
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

canvasXpress(g, width = 672, height = 500)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Position adjustments - No Position
sample_data <- diamonds[sample(nrow(diamonds), 1000), ]
g = ggplot(data = sample_data) + geom_bar(mapping = aes(x = cut, fill = cut))
g

canvasXpress(g, width = 672, height = 500)
Position adjustments - No Position 2
sample_data <- diamonds[sample(nrow(diamonds), 1000), ]
g = ggplot(data = sample_data) + geom_bar(mapping = aes(x = cut, fill = clarity))
g

canvasXpress(g, width = 672, height = 500)
Position adjustments - Fill
sample_data <- diamonds[sample(nrow(diamonds), 1000), ]
g = ggplot(data = sample_data) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
g

canvasXpress(g, width = 672, height = 500)
Position adjustments - Dodge
sample_data <- diamonds[sample(nrow(diamonds), 1000), ]
g = ggplot(data = sample_data) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
g

canvasXpress(g, width = 672, height = 500)
Coordinate systems
g = ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot()
g

canvasXpress(g, width = 672, height = 500)
Vertical Error Bars
df <- ToothGrowth
df$dose <- as.factor(df$dose)
df.summary <- df %>%
group_by(dose) %>%
summarise(
sd = sd(len, na.rm = TRUE),
len = mean(len)
)
f <- ggplot(
df.summary,
aes(x = dose, y = len, ymin = len-sd, ymax = len+sd)
)
v <- f + geom_errorbar(width = 0.2) + geom_point(size = 1.5)
v

canvasXpress(v, width = 672, height = 500)
Horizontal Error Bars
f <- ggplot(
df.summary,
aes(x = len, y = dose, xmin = len-sd, xmax = len+sd)
)
h <- f + geom_point() + geom_errorbarh(height=.2)
h

canvasXpress(h, width = 672, height = 500)
Senssion Info
sessionInfo()
## R version 4.4.0 (2024-04-24)
## Platform: aarch64-apple-darwin20
## Running under: macOS 15.5
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] canvasXpress_1.50.5 lubridate_1.9.3 forcats_1.0.0
## [4] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.4
## [7] readr_2.1.5 tidyr_1.3.1 tibble_3.3.0
## [10] tidyverse_2.0.0 ggplot2_3.5.2
##
## loaded via a namespace (and not attached):
## [1] Matrix_1.7-0 gtable_0.3.6 jsonlite_2.0.0 compiler_4.4.0
## [5] tidyselect_1.2.1 jquerylib_0.1.4 splines_4.4.0 scales_1.4.0
## [9] yaml_2.3.10 fastmap_1.2.0 lattice_0.22-6 R6_2.6.1
## [13] labeling_0.4.3 generics_0.1.4 knitr_1.50 htmlwidgets_1.6.4
## [17] bslib_0.9.0 pillar_1.10.2 RColorBrewer_1.1-3 tzdb_0.4.0
## [21] rlang_1.1.6 cachem_1.1.0 stringi_1.8.7 xfun_0.52
## [25] sass_0.4.10 viridisLite_0.4.2 timechange_0.3.0 cli_3.6.5
## [29] mgcv_1.9-1 withr_3.0.2 magrittr_2.0.3 digest_0.6.37
## [33] grid_4.4.0 rstudioapi_0.16.0 hms_1.1.3 nlme_3.1-164
## [37] lifecycle_1.0.4 vctrs_0.6.5 evaluate_1.0.4 glue_1.8.0
## [41] farver_2.1.2 rmarkdown_2.29 tools_4.4.0 pkgconfig_2.0.3
## [45] htmltools_0.5.8.1