这是 Ben Bolker 建议根据给定状态概率对密度估计区域进行加权的示例(我相信)。我使用 ggplot2 的 weights 参数来做到这一点,似乎需要一些黑客攻击来允许该vioplot
函数允许一个权重函数(尽管这很有用,请参阅有关 crossvalidated 的相关讨论)。
library(ggplot2)
library(reshape2)
state1 <- rnorm(100,5,2)
state2 <- rnorm(100,8,3)
state3 <- rnorm(100,12,0.5)
state1_w <- rep(0.3, 100)
state2_w <- rep(0.2, 100)
state3_w <- rep(0.5, 100)
state_df1 <- data.frame(cbind(state1,state2,state3))
state_df2 <- data.frame(cbind(state1_w,state2_w,state3_w))
#now to reshape and merge
state_melt1 <- melt(state_df1, measure.vars = c("state1","state2","state3"), variable.name = "State_Num", value.name = "State_Value")
state_melt2 <- melt(state_df2, measure.vars = c("state1_w","state2_w","state3_w"), variable.name = "State_W", value.name = "State_WValue")
state_melt <- data.frame(state_melt1,state_melt2)
#now making the plot
p1 <- ggplot(data = state_melt, aes(State_Num,State_Value,weight = State_WValue))
p1 + geom_violin(fill = "green")
您会收到一些错误消息,说权重不加为一,但这里我们希望这些区域与其状态空间概率成正比。