vioplot icon indicating copy to clipboard operation
vioplot copied to clipboard

Add horizontal line?

Open GCAT01 opened this issue 10 months ago • 3 comments

Is there a way to add a horizontal line to your violin plots?

Thank you for this package! I tried making my figure in ggplot and man, was it ugly. Your function looks much better.

GCAT01 avatar Feb 26 '25 14:02 GCAT01

@GCAT01 can you clarify what type of horizontal line you are referring to? For example, does the following satisfy your need?

data <- iris[, 1:3]
vioplot::vioplot(data)
abline(h = mean(data[, 1]), col = "red")

mmahmoudian avatar Feb 26 '25 22:02 mmahmoudian

I was hoping to add a horizontal line at a specific value as if I was using abline.

GCAT01 avatar Feb 27 '25 11:02 GCAT01

I don't believe there is a way to do this with vioplot, but you can always do it manually. For example:

data <- iris[, 1:3]

vioplot::vioplot(data)

# for every column of the data
for(i in seq_len(ncol(data))){
    # draw a line at the mean of each column with the width of
    # 0.5 symmetrically on both sides of the violin
    lines(x = c(i-0.25, i+0.25),
          y = rep.int(mean(data[, i]), 2),
          col = "red")
}

Image

mmahmoudian avatar Feb 27 '25 13:02 mmahmoudian