本文介绍了ggplot小提琴情节不作图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用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)但我明白了
Warning: In max(data$density) : no non-missing argument for max; return -Inf我的情节是
我做错了什么?
推荐答案对于每个 x ,您的数据只有两个 vec_length (y).这实际上是特殊情况".小提琴会缩成一条线的地方.在这种情况下,可以将 geom_violin()也实现为 geom_line(),但这并不是这样实现的:
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()
要拉小提琴,您至少需要三个y值:
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)
更多推荐
ggplot小提琴情节不作图
发布评论