我有一些奇怪的行爲小提琴劇情,當數據是(部分)恆定。小提琴情節以不變的數據?
如果我檢查常量數據並且人爲添加一些小錯誤(例如,通過添加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