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
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:
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: