Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples of overlaying base R graphics to docs #17

Closed
TomKellyGenetics opened this issue Nov 9, 2022 · 0 comments
Closed

Add examples of overlaying base R graphics to docs #17

TomKellyGenetics opened this issue Nov 9, 2022 · 0 comments
Assignees

Comments

@TomKellyGenetics
Copy link
Owner

I've received several related questions which can be resolved by overlaying base R graphics and integrating vioplot with other plotting functions.

"vioplot()" functions similar to "plot()" and passes input arguments from "par()".

For example it is possible to add additional annotations. As requested by email here is an example:

# generate dummy data
a <- rnorm(25, 3, 0.5)
b <- rnorm(25, 2, 1.0)
c <- rnorm(25, 2.75, 0.25)
d <- rnorm(25, 3.15, 0.375)
e <- rnorm(25, 1, 0.25)
datamat <- cbind(a, b, c, d, e)
dim(datamat)
#> [1] 25  5
# violin plot
library("vioplot")
#> Loading required package: sm
#> Warning: package 'sm' was built under R version 4.1.3
#> Package 'sm', version 2.2-5.7: type help(sm) for summary information
#> Loading required package: zoo
#> Warning: package 'zoo' was built under R version 4.1.3
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
vioplot(datamat, ylim = c(0, 5))
# compute medians
data.med <- apply(datamat, 2, median)
data.med
#>         a         b         c         d         e 
#> 3.0475551 2.2365210 2.7504934 3.0730919 0.9803336
#overlay medians
lines(data.med, lty = 2, lwd = 1.5)
points(data.med, pch = 19, col = "red", cex = 2.25)

Created on 2022-11-07 with reprex v2.0.2</sup](https://reprex.tidyverse.org%29%3C/sup)>

It is also possible to modify the axes labels and titles as shown in issue #16

library("vioplot")
#> Loading required package: sm
#> Warning: package 'sm' was built under R version 4.1.3
#> Package 'sm', version 2.2-5.7: type help(sm) for summary information
#> Loading required package: zoo
#> Warning: package 'zoo' was built under R version 4.1.3
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
outcome <- c(rnorm(25, 3, 1), rnorm(25, 2, 0.5))
intervention <- c(rep("treatment", 25), rep("control", 25))
table(intervention)
#> intervention
#>   control treatment 
#>        25        25
names(table(intervention))
#> [1] "control"   "treatment"
unique(sort(intervention))
#> [1] "control"   "treatment"
intervention <- as.factor(intervention)
levels(intervention)
#> [1] "control"   "treatment"
d <- data.frame(outcome, intervention)
vioplot(outcome ~ intervention, data = d, xaxt = 'n', yaxt = 'n', 
        main = "", xlab = "", ylab = "")
axis(side = 1, at = 1:length(levels(intervention)), labels = levels(intervention))
mtext("custom x labels for intervention", side = 1)
mtext("custom y labels for outcome", side = 2)
title(main = "example with custom title", sub = "subtitles are supported")

Created on 2022-11-09 with reprex v2.0.2

I'll share these examples publicly for future reference. This issue indicates these are under consideration in updated documentation or vignettes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant