R ggplot未打印
我试着用R包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
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)
df2norm2
包不是函数吗?使用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)