I try to plot a violin plot with the R package ggplot2 with the code
norm2 = function(v) return(sqrt(sum(v*v)))
myfct = function(d) {
vec_length = Inf
while (vec_length > 1){
vec_length = norm2(runif(n=d,min=-1,max=1))
}
return(vec_length)
}
df = data.frame(x = rep.int(1:5, 2))
df$vec_length = sapply(df$x, myfct)
ggplot(df, aes(factor(x),vec_length)) + geom_violin(trim=FALSE)
but I get
Warning:
In max(data$density) :
no non-missing argument for max; return -Inf
And my plot is

What have I done wrong?
Your data only has two vec_length (y) for each x. This is rather a "special case" where the violin would reduce into a line. One could have implemented geom_violin() also as geom_line() in such cases, but that isn't realized like that:
library(ggplot2)
ggplot(df1, aes(factor(x), vec_length)) + geom_line()

To draw a violin you need at least three y values:
df2 <- data.frame(x=rep.int(1:5, 3))
df2$vec_length <- sapply(df2$x, myfct)
ggplot(df2, aes(factor(x), vec_length)) + geom_violin(trim=FALSE)

library("SpatioTemporal")
set.seed(42)
myfct <- function(d) {
vec_length <- Inf
while (vec_length > 1){
vec_length <- norm2(runif(n=d, min=- 1, max=1))
}
return(vec_length)
}
df1 <- data.frame(x=rep.int(1:5, 2))
df1$vec_length <- sapply(df1$x, myfct)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With