gpt4 book ai didi

r - ggplot fiddle 情节不是在策划

转载 作者:行者123 更新时间:2023-12-05 05:12:45 26 4
gpt4 key购买 nike

我尝试用 R 包 ggplot2 和代码绘制 fiddle 图

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

我的剧情是

enter image description here

我做错了什么?

最佳答案

您的数据对于每个 x 只有两个 vec_length (y)。这是一个“特例”, fiddle 会缩成一条线。在这种情况下,可以将 geom_violin() 也实现为 geom_line(),但这并不是这样实现的:

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

enter image description here

要画 fiddle ,您至少需要三个 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)

enter image description here


数据

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)

关于r - ggplot fiddle 情节不是在策划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com