简体   繁体   中英

How to add total count of values in bar chart y-axis in time-series analysis graph in r

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在此处输入图片说明 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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM