In R, display success / event sequences

EDITED: after recommendation

I am trying to create a simple graph of the frequency of sequential events based on this post: ggplot sequence patterns

I would like to collect the same plot, but for other items (different sequences) Items "aa", "bb", "cc"

An example data set is as follows:

 subject <- c("aa","aa","aa","aa","aa","aa","aa", "bb","bb","bb","bb","bb","bb","bb","cc","cc","cc","cc","cc","cc","cc") event <- c("P","C","D","C","P","E","D","C","P","E","D","P","C","E","P","E","D","P","C","x","x") freq <- c(3,2,1,4,2,1,0,3,4,1,3,3,1,2,1,3,2,1,4,0,0)) dfx <- data.frame(subject, event, freq) 

As a result, I get: my result, bad

Using this code based on the original message:

 library(ggplot2) dfx$type <- factor(dfx$type) dfx$ymin <- c(0,1,2) dfx$ymax <- c(1,2,3) dfx$xmax <- cumsum(dfx$count) dfx$xmin <- c(0, head(dfx$xmax, n=-1)) plot_x <- ggplot(dfx, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,fill=type)) +geom_rect(colour="grey40", size=0.5) png("plot_x.png", height=200, width=800) print(plot_x) dev.off() 

I created this image with what I would like to build. (handmade in excel). In this case, we have 3 objects, 4 events (C, P, D, E) + 1 dummy event (X) needed to create a data frame. As you can see, the total number of events is not necessarily the same for each item. expected

+6
source share
1 answer

Try using facet_grid or facet_wrap

 library(ggplot2) p1 <- ggplot(dfx, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = event))+ theme_bw() + geom_rect(colour = "black", size = 0.5) + facet_grid(subject ~ .) + theme(axis.ticks.y=element_blank(), axis.text.y=element_blank()) p1 

enter image description here DATA:

 dfx <- data.frame( subject = c("aa","aa","aa","aa","aa","aa","aa", "bb","bb","bb","bb","bb","bb","bb","cc","cc","cc","cc","cc","cc","cc"), event = c("P","C","D","C","P","E","D","C","P","E","D","P","C","E","P","E","D","P","C","x","x"), freq = c(3,2,1,4,2,1,0,3,4,1,3,3,1,2,1,3,2,1,4,0,0), ymin = 0, ymax = 1) TEMP <- tapply(dfx$freq,dfx$subject,cumsum) dfx$xmax <- unlist(TEMP) dfx$xmin <- c(0, head(TEMP$aa, -1), 0, head(TEMP$bb, -1), 0, head(TEMP$cc, -1)) 
+1
source

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


All Articles