ggplot小提琴情节不作图 [英] ggplot violin plot is not plotting

查看:37
本文介绍了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小提琴情节不作图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆