1
votes

I have a dataset that looks something like this:

testSet <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04",
                                     "2013-10-05","2013-11-06")), 
                      yr = c(2013,2013,2013,2013,2013), 
                      mo = c(07,08,09,10,11), 
                      da = c(02,03,04,05,06), 
                      plant = LETTERS[1:5], 
                      product = letters[26:22], 
                      rating = runif(5))

What I would like to do is plot 2 graphs using ggplot2.

The first would give me a dodged, continuous bar chart for all months that would have the product on the x, the ratings on the y, and the dates grouped and plotted on their respective products.

x = product

y = rating

Dodge = date

The second that I'm trying to create is a dodged, continuous bar chart for one month that would have the plant on the x, the ratings on the y, and the product grouped and plotted on their respective plants.

x = plant

y = rating

Dodge = product

I'm looking for an output that is very similar to this: http://docs.ggplot2.org/0.9.3/geom_bar-28.png but continuous.

I've had issues trying to figure out how the levels things works and haven't seen an example of a dodged, continuous chart.

Here is the code I have created so far:

testMean <- tapply(testSet$rating, list(testSet$mo), mean)

testLevels <- factor(levels(testSet$product,testSet$mo), 
                  levels = levels(testSet$product,testSet$mo))

qplot(testLevels, aes(testMean, fill=cut)) +
          geom_bar(position="dodge", stat="identity")

This is what the ggplot2 site says about creating a continuous bar chart, but it doesn't say anything about how to do it with multiple graphs overlayed on top of each other and then dodged, like in the one I linked to earlier. Here is their code:

meanprice <- tapply(diamonds$price, diamonds$cut, mean)

cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut))
qplot(cut, meanprice)

I appreciate the help, guys!

1
hi there, have you tested the code you posted? I am not sure what you are doing there with the levels(), but it does not make sense. What were you hoping for testLevels to be? Also, it appears you are confusing data.table with data.frame - Ricardo Saporta
I added new information to the posting as well. Basically, that's how the ggplot2 site has that specific graph set up. However, it is only doing one graph, whereas I want to overlay all of the graphs onto one graph. - black_sheep07
And the code I posted does not work. I get a failure when creating testLevels. - black_sheep07
@black_sheep07 your question is not clear. Could you provide a better dataset? - Paulo E. Cardoso

1 Answers

0
votes

I ended up using the diamonds built in data set to get my question answered. All the thanks in the world to @carloscinelli for his assistance.

library(data.table)
data <- data.table(diamonds)[,list(mean_carat=mean(carat)), by=c('cut', 'color')]

Thanks!