ggplot小提琴情节不作图

编程入门 行业动态 更新时间:2024-10-09 10:31:55
本文介绍了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小提琴情节不作图

本文发布于:2023-07-18 08:17:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142764.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小提琴   情节   ggplot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!