Ggplot sequence sequences

I am trying to build a sequence of colored small squares representing various activities. For example, in the next data frame, the type represents the type of activity and count represents how many of these actions happened before the "other typed" event occurred.

df3 <- data.frame(type=c(1,6,4,6,1,4,1,4,1,1,1,1,6,6,1,1,3,1,4,1,4,6,4,6,4,4,6,4,6,4), count=c(6,1,1,1,2,1,6,3,1,6,8,10,3,1,2,2,1,2,1,1,1,1,1,1,3,3,1,17,1,12) ) 

In ggplot, I currently do not use count. I just specify consecutive numbers as xvalues ​​and 1 as yvalues. However, this gives me something like ggplot Image. This is the code I used, note that for y I always use 1, and for x I only use consecutive numbers:

  ggplot(df3,aes(x=1:nrow(df3),y=rep(1,30))) + geom_bar(stat="identity",aes(color=as.factor(type))) 

I would like to get small squares with the graph = df3 $ count.

Do you have any suggestions? thanks in advance

+2
source share
1 answer

I do not quite understand what you need, but I suggest one of the possible ways to build your data. I used geom_rect() to draw rectangles with a width equal to your count column. Rectangles are displayed in the same order as the rows of your data.

 df3 <- data.frame(type=c(1,6,4,6,1,4,1,4,1,1,1,1,6,6,1, 1,3,1,4,1,4,6,4,6,4,4,6,4,6,4), count=c(6,1,1,1,2,1,6,3,1,6,8,10,3,1,2, 2,1,2,1,1,1,1,1,1,3,3,1,17,1,12)) library(ggplot2) df3$type <- factor(df3$type) df3$ymin <- 0 df3$ymax <- 1 df3$xmax <- cumsum(df3$count) df3$xmin <- c(0, head(df3$xmax, n=-1)) plot_1 <- ggplot(df3, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, fill=type)) + geom_rect(colour="grey40", size=0.5) png("plot_1.png", height=200, width=800) print(plot_1) dev.off() 

enter image description here

+2
source

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


All Articles