Forum Discussion

palvarez's avatar
palvarez
New Member
8 years ago
Solved

R Script ggplot Line Plot

Hello,   I'm trying for the first time ever R Scripting with ggplot. However I've encountered a small roadblock. While attempting to do a line chart, why does my data plunges to 0 but lines back to...
  • v-sihou-msft's avatar
    8 years ago

    palvarez

     

    Based on your code, it seems you put date field into your dataset. I assume you have data on day level. As you format your Date into "YearMonth", which means you have multiple data point on same X axis category. This the reason why your line goes crazy. 

     

    In your scenario, you should use stat_summary() function to have those data points aggregate on each "YearMonth". Please refer to my sample below: 

     

    library(ggplot2)
    library(gtable)
    
    df <- dataset
    trans <- df$Amount
    fec <- format(as.Date(df$Date), "%Y/%m")
    options(scipen=999)
    p1 <- ggplot() +stat_summary(aes(x=fec, y=trans,group=1), fun.y = sum, geom = "line")
    p1

     

    Regards,