0

Here is a sample of my dataset and I am attempting to create a bar chart using ggplot2 that shows the top N problems for each month. Using the sample dataset I would like to plot the top 3 problems for April 2018.

Month_Year  Problem             Problem_Count
April 2018  Parental consent    222
April 2018  Unable to download  53
April 2018  App restriction     105
April 2018  Website issue       99
April 2018  Account issue       17

Here is the code I have so far (it is based on the this question)

require(reshape2)
require(ggplot2)

ordered.results <- dataset$Problem_Count[order(dataset$Problem_Count, decreasing = TRUE)]

dfm <- melt(dataset, id.vars = c('Month_Year', 'Problem'))
dfm

csat <- ggplot(data = subset(dfm, Problem %in% ordered.results[1:5]) , aes(x=factor(Problem), y = value, fill = variable))
csat <- csat + geom_bar(stat='identity', width = 0.5, position = 'dodge')
csat <- csat + facet_grid(. ~ Month_Year)
csat <- csat + theme_bw()
csat <- csat + theme(axis.text.x = element_text(angle = 90))
csat

The issue I am having is being able to only plot the top N of each problem by month. Using this code I get the error Error: Faceting variables must have at least one value. Any help is much appreciated as I am very inexperienced with R. Thanks!

camille
  • 16,432
  • 18
  • 38
  • 60
Leif
  • 125
  • 9
  • 1
    Please share sample of your data using `dput()` (not `str` or `head` or picture/screenshot) so others can help. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Aug 07 '18 at 17:34
  • Your `subset()` code doesn't return anything (run it outside of `ggplot()` to see. The `ordered.result` you made is based on the count, so maybe you want `subset(dfm, value %in% ordered.results[1:5])`? – aosmith Aug 07 '18 at 17:40
  • @Tung I will take a look at getting it properly formatted. Thank you for the link. – Leif Aug 07 '18 at 17:41
  • @aosmith Fixed the issue. Thank you very much! – Leif Aug 07 '18 at 17:42
  • @aosmith I would like to give you credit for the answer, but it appears I am unable to so for an answer in the comments. – Leif Aug 07 '18 at 17:45
  • Thanks for wanting to give credit! You should go ahead and put in an answer with the code that fixed things so you can mark this as answered. – aosmith Aug 07 '18 at 17:56

1 Answers1

0

Credit for this goes to @aosmith. The issue was I was calling Problem and not value, which is part of melt(). Here is the fixed code.

ordered.results <- dataset$Problem_Count[order(dataset$Problem_Count, decreasing = TRUE)]

dfm <- melt(dataset, id.vars = c('Month_Year', 'Problem'))
dfm

csat.plot <- ggplot(data = subset(dfm, value %in% ordered.results[1:5]) , aes(x=factor(Problem), y = value, fill = variable))
csat.plot <- csat.plot +geom_bar(stat='identity', width = 0.5, position = 'dodge')
csat.plot <- csat.plot + facet_grid(. ~ Month_Year)
csat.plot <- csat.plot + theme_bw()
csat.plot <- csat.plot + theme(axis.text.x = element_text(angle = 90))
csat.plot
Leif
  • 125
  • 9