我有一些奇怪的行为小提琴剧情,当数据是(部分)恒定。小提琴情节以不变的数据?
如果我检查常量数据并且人为添加一些小错误(例如,通过添加runif(N, min = -0.001, max = 0.001)
,脚本将运行,但是,这会将其他小提琴图扭曲到垂直线(参见1),而它应该是这个样子2
问:
是否有可能(当小提琴剧情部分数据是不变的),以
- 显示相应常量数据的简单水平线
- 显示其他小提琴图,就好像常数不存在一样?
R代码里面:
library(ggplot2)
library(grid)
library(gridExtra)
N <- 20
test_data <- data.frame(
idx <- c(1:N, 1:N),
vals <- c(runif(N, 0, 1),
rep( 0.5, N)), # <- R script won't run
#rep(0.5, N) + runif(N, min = -0.001, max = 0.001)), # <- delivers graphic (distorted)
type <- c(rep("range", N),
rep("const", N))
)
grid.arrange(
ggplot(test_data, aes(x = idx, y = vals)) +
geom_line(aes(colour = type)),
ggplot(test_data, aes(x = type, y = vals)) +
geom_violin(aes(fill = type),
position = position_dodge(width = 1))
)
答案为[这个问题](http://stackoverflow.com/questions/24129772/ggplot2-geom-violin-wit h-0-variance)给出了这个问题的一些选择。如果方差大于0,您可以为组变量添加一个变量到数据集中,然后在'ggplot'中将数据集进行子集化。使用'dplyr'添加此变量:'test_data = test_data%>%group_by(type)%> %mutate(vars = var(vals))'。 – aosmith