我有一个显示三种状态的vioplot:
library(vioplot)
state1 <- rnorm(100,5,2)
state2 <- rnorm(100,8,3)
state3 <- rnorm(100,12,0.5)
vioplot(state1,state2, state3, names=c("a", "b", "c"), col="green")
如何添加到此图中以图形方式显示 state1发生的概率是0.3, state2为0.2,state3为0.5?
或者是否有更好的方式以图形方式表示?
感谢您的帮助。
答案 0 :(得分:2)
text(x=(1:3)+.2, y=-0.5,
labels=paste0( "prob=\n",
round( sapply(list(state1, state2, state3), mean)/
length(c(state1, state2, state3))
,2) ) )
答案 1 :(得分:2)
以下是Ben Bolker关于根据给定的状态概率对密度估计区域进行加权的建议(我相信)。我使用权重参数ggplot2来执行此操作,看起来需要一些黑客来允许vioplot
函数允许权重函数(尽管这很有用,请参阅related discussion on 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")
您将收到一些错误消息,指出权重不会添加到一个,但在这里我们希望区域与其状态空间概率成比例。