rtime-seriesdygraphsrchartshtmlwidgets

Representing time series data based on multiple classifications in R


So here goes my first ever post in SO. I have a data set which looks something as shown below. But it is for many more buckets and for a higher duration of time period. I am looking to get some kind of an interactive plot that would be ideal to represent this data. The representation needs to take into account all the mentioned columns and needs to be interactive. I tried going through packages such as dygraphs, ggplot2, rcharts and some others, but failed to find anything easy and convenient. I am just starting off with R, so some insights would be great.

Month    Age    Gender  Percentage
Mar-16  0-20    F         1.01
Mar-16  0-20    M         0.46
Mar-16  21-30   F         5.08
Mar-16  21-30   M         4.03
Apr-16  0-20    F         2.34
Apr-16  0-20    M         3.55
Apr-16  21-30   F         6.78
Apr-16  21-30   M         9.08
May-16  0-20    F         3.56
May-16  0-20    M         3
May-16  21-30   F         2.08
May-16  21-30   M         10

Solution

  • Here's a quick visualisation with ggplot2 and plotly as suggested by @KppatelPatel The output of ggplotly will be interactive plot on your graphical user interface, with hover-over information e.g. Month: Apr-16; Percentage: 2.34; Gender: F

    library(ggplot2)
    library(plotly)
    
    p <- ggplot(dat, aes(x=Month, y=Percentage, fill=Gender)) + 
      geom_bar(stat="identity", position = position_dodge()) + 
      facet_wrap(~Age, ncol=2)
    
    ggplotly(p)
    

    enter image description here

    The data.frame dput for the provided data:

    dat <- structure(list(Month = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 
    1L, 3L, 3L, 3L, 3L), .Label = c("Apr-16", "Mar-16", "May-16"), class = "factor"), 
    Age = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 
    2L, 2L), .Label = c("0-20", "21-30"), class = "factor"), 
    Gender = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L), .Label = c("F", "M"), class = "factor"), Percentage = c(1.01, 
    0.46, 5.08, 4.03, 2.34, 3.55, 6.78, 9.08, 3.56, 3, 2.08, 
    10)), .Names = c("Month", "Age", "Gender", "Percentage"), class = "data.frame", row.names = c(NA, 
    -12L))
    

    EDIT:

    To plot time in logical order, convert Month to date format:

    library(dplyr)
    dat$Time <- dat$Month %>% 
                as.character %>%
                paste("01-", .) %>%
                as.Date(., format= "%d-%b-%y")
    

    Plotting with x=Time for the same ggplot above will give you the following:

    enter image description here