1

I'm creating a shiny application that will have a checkboxGroupInput, where each box checked will add another line to a frequency plot. I'm trying to wrap my head around reshape2 and ggplot2 to understand how to make this possible.

data:

  head(testSet)
  date store_id product_id count
1 2015-08-15      3       1     8
2 2015-08-15      3       3     1
3 2015-08-17      3       1     7
4 2015-08-17      3       2     3
5 2015-08-17      3       3     1
6 2015-08-18      3       3     2

class level information:

dput(droplevels(head(testSet, 10)))
structure(list(date = structure(c(16662, 16662, 16664, 
16664, 16664, 16665, 16665, 16665, 16666, 16666), class = "Date"), 
    store_id = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), product_id = c(1L, 
    3L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 2L), count = c(8L, 1L, 7L, 
    3L, 1L, 2L, 18L, 1L, 0L, 2L)), .Names = c("date", "store_id", 
"product_id", "count"), row.names = c(NA, 10L), class = "data.frame")

The graph should have an x-axis that corresponds to date, and a y-axis that corresponds to count. I would like to have a checkbox group input where for each box representing a product checked, a line corresponding to product_id will be plotted on the graph. The data is already filtered to store_id.

My first thought was to write a for loop inside of the plot to render a new geom_line() per each returned value of the input$productId vector. -- however after some research it seems that's the wrong way to go about things.

Currently I'm trying to melt() the data down to something useful, and then aes(...group=product_id), but getting errors on whatever I try.

Attempting to melt the data:

meltSet <- melt(testSet, id.vars="product_id", value.name="count", variable.name="date")

head of meltSet

  head(meltSet)
  product_id date count
1       1 date 16662
2       3 date 16662
3       1 date 16664
4       2 date 16664
5       3 date 16664
6       3 date 16665

tail of meltSet

   tail(meltSet)
   product_id date count
76       9      count     5
77       1      count    19
78       2      count     1
79       3      count    39
80       8      count     1
81       9      count     4

Plotting:

ggplot(data=meltSet, aes(x=date, y=count, group = product_id, colour = product_id)) + geom_line()

enter image description here

So my axis and values are all wonky, and not what I'm expecting from setting the plot.

3
  • melt is the right idea. It's very hard to know what you're doing wrong when you don't show your code or even the text of the errors! Please edit to show exactly the code you are using for melt(), maybe the head() of the melted data frame, and a (simple) version of your ggplot code, along with the full text of any errors you get. It would also be nice if you shared your initial data with dput() e.g., dput(droplevels(head(testSet, 10))), so that the class information of the columns is shared as well. It will matter if you are using factors or Dates or numerics or characters. Commented Sep 15, 2016 at 23:18
  • I've removed the shiny related tags - it seems like shiny is your end goal but this is just an intermediate step. The fact that this will end up in a shiny app seems tangential. Commented Sep 15, 2016 at 23:22
  • Thanks @Gregor , I have updated the question with my most recent attempt. Will be keeping at it, I'm sure I"m just not melting my data properly. Commented Sep 15, 2016 at 23:45

1 Answer 1

1

If I'm understanding it correctly you don't need any melting, you just need to aggregate your data, summing up count by date and product_id. you can use data.table for this purpose:

testSet = data.table(testSet)
aggrSet = testSet[, .(count=sum(count)),  by=.(date, product_id)]

You can do your ggplot stuff on aggrSet. It has three columns now: date, product_id, count.

When you melt like you did you merge two variables with different types into date: date(Date) and store_id(int).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.