R Representation of state probabilities in violota

I have a vioplot showing three states:

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") 

how can I add a graphical display to this graph that the probability of occurrence of state 1 is 0.3, state2 is 0.2, and state 3 is 0.5โ‰ค

Or is there a better way to present this graphically?

Thank you for your help.

+2
source share
2 answers

Here is an example (I think) of Ben Bolcker's proposal for weighted density estimates in accordance with given state probabilities. I used the weight argument for ggplot2 to do this, it seems like some hacking is needed for the vioplot function vioplot allow the use of the weight function (although that would be useful, see ) on a cross base ).

enter image description here

 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") 

You will get several error messages saying that weights are not added to one, but here we want the areas to be proportional to their spatial probabilities.

+2
source
  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) ) ) 
+2
source

Source: https://habr.com/ru/post/1479347/


All Articles