2

I found a great linear programming example using lpsolveapi. It was mentioned on R bloggers, and the link to the original post can be found here. The Rscript can be downloaded from Github here.

The problem is that the code was based on a version of ggplot2 pre version 0.9.1. So when running the example, the error message is Error: Use 'theme' instead. (Defunct; last used in version 0.9.1).

On CRAN, the suggestion is:

Error : Mapping a variable to y and also using stat="bin".
 With stat="bin", it will attempt to set the y value to the count of cases in each group.
 This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
 If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
 If you want y to represent values in the data, use stat="identity".
 See ?geom_bar for examples. (Defunct; last used in version 0.9.2)

I understand that the code has to reformulated based on the more recent version of ggplot2, but that's where I get stuck. Being the beginner that I am, I don't know where to start. I tried not assigning to y and I tried using stat='bin' or stat='identity'. Instead of me posting my messing code with errors, I will rather ask if the outdated file could be updated.

Here is a section of the code, that if updated, I could replicate to the other sections:

results<-data.frame(cargo=rep(cargo$type, 3), 
wagon=as.vector(sapply(train$wagon,     
FUN=function(x) rep(x, NROW(cargo)))), solution=get.variables(lpmodel))

r1<-ggplot(results, aes(x=cargo, y=solution, fill=wagon)) + 
    geom_bar(color='black', position='dodge') + 
    geom_text(aes(label=solution), size=2.5, position=position_dodge(width=1), vjust=-.4) + 
    scale_fill_brewer(palette='Set1') + 
    facet_grid(.~wagon) + 
    opts(title='Planning result', legend.position='none') + 
    ylab('Solution (tonnes)')
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
user3580643
  • 111
  • 3
  • 11
  • You might want to have a look at the [Graph section of the R Cookbook](http://www.cookbook-r.com/Graphs/). It's an excellent resource about how to use `ggplot2`. You can't use `opts` anymore. With the `theme` function you can change the appearance of your plot. – Jaap May 26 '14 at 08:21
  • 2
    It should be straight-forward: change all occurrences of `opts()` to `theme()`. Good luck. – Andrie May 26 '14 at 09:38
  • Try this: r1 <- ggplot(results, aes(x=cargo, y=solution, fill=wagon)) + geom_bar(color='black', position='dodge', stat="identity") + geom_text(aes(label=solution), size=2.5, position=position_dodge(width=1), vjust=-.4) + scale_fill_brewer(palette='Set1') + facet_grid(.~wagon) + ggtitle('Planning result') + theme(legend.position='none') + ylab('Solution (tonnes)') – Andrie May 26 '14 at 09:45
  • Thanks your method was 90% of the way there. vrajs5's solution build upon yours, and is correct – user3580643 May 26 '14 at 13:25

1 Answers1

3

Try this... It looks like what you have reffered.

r1<-ggplot(results, aes(x=cargo, y=solution, fill=wagon)) + 
  geom_bar(color='black', position='dodge', stat='identity') + 
  geom_text(aes(label=solution), size=2.5, position=position_dodge(width=1), vjust=-.4) + 
  scale_fill_brewer(palette='Set1') + 
  facet_grid(.~wagon) + 
  theme(title=element_text('Planning result'), legend.position='none') + 
  ylab('Solution (tonnes)')
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
vrajs5
  • 4,066
  • 1
  • 27
  • 44