Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot未打印_R_Ggplot2 - Fatal编程技术网

R ggplot未打印

R ggplot未打印,r,ggplot2,R,Ggplot2,我试着用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)) d

我试着用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\u长度
(y)。这是一种“特殊情况”,小提琴会缩成一条线。在这种情况下,可以将
geom_小提琴()
也实现为
geom_线()
,但这并不是这样实现的:

library(ggplot2)
ggplot(df1, aes(factor(x), vec_length)) + geom_line()

要绘制小提琴,至少需要三个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)

df2
norm2
包不是函数吗?使用
base::norm
抛出error@FonsMA:抱歉,添加了函数。@RLave:抱歉,添加了函数。
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)