Search code examples
rline-plotbinslongitudinal

Bins for fixed interval in Longitudinal data and plotting it over the period of time by categories


It is longitudinal data; ID wise values are repeating 4 times in every tick of 20 steps. Then this experiments repeats. For the datafarme below I want bins based for every tick time steps for the categories of land based on the values of X. Bins can be 3 for every time interval for land type (Small, medium and large) each. I want to see timeline of bins of X based on categories of Land. Any help will be appreciated. I have added possibly a picture of how data may look like for ggplot and plot as bins or dots may look like as in picture.

   Seed(123)
ID = 1:5
Time = rep (c(1,2,3,4,5), each = 20)
Type = 1:25
data <- data.frame( IDn = rep(ID,20), Time,  Land = rep(Type, 40), y = rnorm(100,0,1), x = runif(100,0,1))
data$Land= ifelse (data$Land > 15,"large farmers", ifelse(data$Land <=5, "small farmers", "medium-farmers"))

enter image description here

enter image description here

Edit: Question for labeling the faceting variable and dot plots.

enter image description here


Solution

  • Maybe something like this would help -

    library(dplyr)
    library(ggplot2)
    
    data %>%
      group_by(Time, Land) %>%
      mutate(x = cut(x, c(0, 0.25, 0.75, 1))) %>%
      ungroup %>%
      count(Time, Land, x) %>%
      ggplot() + aes(Time, n, fill = Land) + geom_col(position = 'dodge')