1

I have a dataset in this format.

head(sppec.p)
    Wavelength_.nm.      Mean     Lower     Upper Species Feature_selection Count
1             400 0.1467549 0.1290778 0.2758327    ALAL            T-test     0
2             405 0.1454303 0.1271905 0.2726207    ALAL            T-test     0
3             410 0.1464431 0.1290472 0.2754903    ALAL            T-test     0
4             415 0.1468586 0.1298166 0.2766753    ALAL            T-test     0
5             420 0.1485061 0.1310419 0.2795480    ALAL            T-test     0
6             425 0.1517019 0.1342690 0.2859708    ALAL            T-test     0

I originally planned on doing a dual-axis graph, but after searching here, I realized that it was "not possible" in ggplot and many advised against such plots. Therefore I have settled for a facet plot. I tried to do the individual plots first but I cannot figure out how to use facet_wrap and scales = "free_y" to get the 2 plots of identical x-axis but different y-axis.

Code for the individual graphs:

  myplot<-ggplot(sppec.p, aes(x=Wavelength_.nm., y=Mean, group=Species, linetype =   Species,
ymin = Lower, ymax =  Upper)) +
 geom_ribbon(alpha = 0.2,fill='gray') +
 geom_line() +  theme_bw() +scale_fill_grey(start = 0.1, end = 0.9) +
 opts(panel.background = theme_rect(fill='grey80')) +
 ylim(0,3)

myplot + theme_bw ()+ opts(axis.line = theme_segment(colour = "black"),
  panel.grid.major = theme_blank(),
  panel.grid.minor = theme_blank()) 

...which produces enter image description here

And the second graph:

  cum.totals<-ggplot(sppec.p, aes(Wavelength_.nm., y=Count, fill =     factor(Feature_selection))) + geom_bar(stat="identity")
   cum.totals + theme_bw() +scale_fill_grey(start = 0.1, end = 0.9)+
opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),  
panel.grid.minor = theme_blank(),
panel.border = theme_blank(),
panel.background = theme_blank())

Graph produced:

enter image description here

Though both y-axis appear numerically equivalent, they represent different values.

I am not very familiar with ggplot and I would like to know how to use facet_wrap or any other function that would give me the 2 plots on the sharing the same a-axis but in 2 facets. Is it possible to make my code more efficient?

So essentially the layout of the final output would be something like this: enter image description here

user2507608
  • 355
  • 1
  • 6
  • 18
  • Are you trying to get two y axes for count and mean plotted against the same x axis for wavelength? – TheComeOnMan Nov 26 '13 at 03:18
  • Essentially yes. Something similar to the response posted by Mark Parker found [here](http://stackoverflow.com/questions/12448428/align-multiple-ggplot2-plots-with-grid). But I would need the relevant legends next to the corresponding plots. – user2507608 Nov 26 '13 at 03:41
  • Like i mentioned in my answer, the two y values in your question are not comparable, whereas in the linked question, they probably are. Therefore plotting the both of them in the same chart is not a good pactice. And Matt's answer uses `facet` which is probably not the right trick here as explained in my answer as well. – TheComeOnMan Nov 26 '13 at 03:50
  • I think I found the solution to a similar problem [here](http://stackoverflow.com/questions/14590111/how-to-control-plot-width-in-gridextra) – user2507608 Nov 26 '13 at 04:18
  • See this [answer](http://stackoverflow.com/a/25451066/680068) – zx8754 Aug 22 '14 at 16:01

1 Answers1

1

Facet is useful when you have the same type of chart broken down into multiple categories. Your situation is different. Check out the help entry on the various facet_grid and facet_wrap to see the difference. Here's something to help you get started -

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl)

enter image description here

i'm not a big fan of two different y axes against the same x axis (which is what you're trying to do there, if I understand correctly). Instead, try gridExtra::arrangeGrob with the argument ncol = 1 to have your charts stacked vertically on top of another allowing you to have identical x axes on top of one another and achieving a similar effect.

TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54
  • Thanks for the suggestion. I will try to search for examples on how to use `gridExtra::arrangeGrob`. Just for clarification, I do not want a dual y-axis graph (one graph with both the vertical bars chart and the line graph). For example, instead of having one row and 3 columns, as in the example you have, I am seeking 2 rows and one column (if it makes more sense to you???) – user2507608 Nov 26 '13 at 04:01
  • I just remembered, you could also try plain ol' `rbind`. – TheComeOnMan Nov 26 '13 at 04:11
  • `rbind` on the ggplot objects? That would be interesting – user2507608 Nov 26 '13 at 04:17
  • Check this - http://stackoverflow.com/questions/16255579/how-can-i-make-consistent-width-plots-in-ggplot-with-legends – TheComeOnMan Nov 26 '13 at 04:19