0

I have created a graph from two variables (customer.complaints and Date_by_month) for the trend chart of monthly granularity levels but on the y-axis, it's showing me the name of the complaints, instead of that I want the total number of complaints in that particular month. This is the graph I am getting enter image description here as you can see the value of the y-axis it quite annoying. instead of that, I want to add the count of the values Here is my code snippet

library(ggplot2)
library(scales)

### Provide the trend chart for the number of complaints at monthly and daily granularity levels.
# converting date var to date type 
comcast$Date <- gsub('-', '/', comcast$Date)
comcast$Date <- as.Date(comcast$Date, '%d/%m/%Y')

# plotting graph for monthly granularity levels
comcast$Date_by_month <- as.Date(cut(comcast$Date, breaks='month'))
ggplot(comcast, aes(Date_by_month, Customer.Complaint)) + stat_summary(fun.y=sum, geom='bar') + scale_x_date(labels=date_format("%Y-%m"), breaks='1 month') + scale_y_continuous(labels = fun.y= length)
Darkstar Dream
  • 1,649
  • 1
  • 12
  • 23
  • 1
    Hello! Please can you provide some [reproductible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) data to work with? If they are confidential, can you reproduce the problem with any base R datasets (like `mtcars`)? I have a feeling it can be solved with `geom_text()` or some data pre-formatting. – Paul Feb 28 '20 at 11:05

1 Answers1

0

There are a few issues here. The code you posted actually has a syntax error in the last line so doesn't run at all : scale_y_continuous(labels = fun.y= length), so whatever code produced your plot, it wasn't the code you posted.

In the line stat_summary(fun.y=sum, geom='bar') you are asking to get a sum of a text variable, which doesn't make any sense (maybe you meant count or length?)

And, of course, your problem isn't reproducible because you haven't given us any data to try it out on.

That said, let's recreate a similar data frame:

library(ggplot2)
library(lubridate)
library(scales)

random_words  <- function(x) paste0(sample(c(" ", " ", letters), 50, TRUE), collapse = "")
Date_by_month <- as.Date(as.POSIXct("2015-01-01") + months(sample(12, 100, TRUE)))
complaints    <- sapply(1:50, random_words)

comcast <- data.frame(Date_by_month = as_date(Date_by_month), Customer.Complaint = complaints) 

head(comcast)
#>   Date_by_month                                 Customer.Complaint
#> 1    2015-09-30 impzvcx esxfmknrpufewh   fxqknamay qhob cvpzlgubpu
#> 2    2015-08-31 mwt aezkcolutpengovtggeqavkxnfr myrq famttzzurj ug
#> 3    2015-04-30 uusewv wjxdpywsssqxgclhmlksrxqnqdfsip u jrdsfbldey
#> 4    2015-08-31 sf jytjtwseahfaqtvzisozuhhtrzygysxndyjifxoaytxhncf
#> 5    2015-06-30 vabtbfijnkeflhgpsspxyasiistuqqqjxuqs bsucp lbdrgbn
#> 6    2015-03-01 eylmurltlfgcp rvfdx as hiehnqdrn lrqanrmf quvzbhgh

Now, we don't actually need to include the complaints themselves in the chart. We can just give the dates to ggplot and it will automatically perform counts if we select geom_bar:

ggplot(comcast, aes(Date_by_month)) + 
  geom_bar() +
  scale_x_date(labels = date_format("%Y-%m"), breaks='1 month')

Created on 2020-02-28 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87