[英]ggplot violin plot is not plotting
I try to plot a violin plot with the R package ggplot2
with the code 我尝试使用代码R包
ggplot2
绘制小提琴图
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
. 您的数据每个
x
仅具有两个vec_length
(y)。 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: 在这种情况下,可以将
geom_violin()
也实现为geom_line()
,但是并没有这样实现:
library(ggplot2)
ggplot(df1, aes(factor(x), vec_length)) + geom_line()
To draw a violin you need at least three y values: 要拉小提琴,您至少需要三个y值:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.