Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Is this what you want?</p> <pre><code>library(ggplot2) library(plyr) library(gridExtra) # create data that links colour per 'cat' with 'fishery' # the 'cat' colours will be used as manually set fill colours. # get 'cat' colours # alt. 1: grab 'cat' colours from plot object # create a plot with fill = fishery *and* colour = cat g1 &lt;- ggplot(df, aes(x = year, y = TOTALshark, fill = fishery, colour = cat)) + geom_bar(width = 0.5, stat = "identity", position = "dodge") + facet_wrap(~ div) g1 # grab 'cat' colours for each 'fishery' from plot object # to be used in manual fill cat_cols &lt;- unique(ggplot_build(g1)[["data"]][[1]]$colour) # unique 'cat' cat &lt;- unique(df$cat) # create data frame with one colour per 'cat' df2 &lt;- data.frame(cat = cat, cat_cols) df2 # alt 2: create your own 'cat' colours # number of unique 'cat' n &lt;- length(cats) # select one colour per 'cat', from e.g. brewer_pal or other palette tools cat_cols &lt;- brewer_pal(type = "qual")(n) # cat_cols &lt;- rainbow(n) # create data frame with one colour per 'cat' df2 &lt;- data.frame(cat, cat_cols) df2 # select unique 'fishery' and 'cat' combinations # in the order they show up in the legend, i.e. ordered ('arranged') by fishery df3 &lt;- unique(arrange(df[, c("fishery", "cat")], fishery)) df3 # add 'cat' colours to 'fishery' # use 'join' to keep order df3 &lt;- join(df3, df2) df3 # plot with fill by 'fishery' creates a fill scale by fishery, # but colours are set manually using scale_fill_manual and the 'cat' colours from above g2 &lt;- ggplot(df, aes(x = year, y = TOTALshark, fill = fishery)) + geom_bar(width = 0.5, stat = "identity", position = "dodge") + facet_wrap(~ div, nrow = 5) + scale_fill_manual(values = as.character(df3$cat_cols)) g2 </code></pre> <p><img src="https://i.stack.imgur.com/n3nkc.png" alt="enter image description here"></p> <pre><code># create plot with both 'fishery' and 'cat' legend. # extract 'fisheries' legend tmp &lt;- ggplot_gtable(ggplot_build(g2)) leg &lt;- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend_fish &lt;- tmp$grobs[[leg]] # create a non-sense plot just to get a 'fill = cat' legend g3 &lt;- ggplot(df, aes(x = year, y = TOTALshark, fill = cat)) + geom_bar(stat = "identity") + scale_fill_manual(values = as.character(df3$cat_cols)) # extract 'cat' legend tmp &lt;- ggplot_gtable(ggplot_build(g3)) leg &lt;- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend_cat &lt;- tmp$grobs[[leg]] # arrange plot and legends library(gridExtra) # quick and dirty with grid.arrange # in the first column, put the plot (g2) without legend (removed using the 'theme' code) # put the two legends in the second column grid.arrange(g2 + theme(legend.position = "none"), arrangeGrob(legend_fish, legend_cat), ncol = 2) # arrange with viewports # define plotting regions (viewports) grid.newpage() vp_plot &lt;- viewport(x = 0.25, y = 0.5, width = 0.5, height = 1) vp_legend &lt;- viewport(x = 0.75, y = 0.7, width = 0.5, height = 0.5) vp_sublegend &lt;- viewport(x = 0.7, y = 0.25, width = 0.5, height = 0.3) print(g2 + theme(legend.position = "none"), vp = vp_plot) upViewport(0) pushViewport(vp_legend) grid.draw(legend_fish) upViewport(0) pushViewport(vp_sublegend) grid.draw(legend_cat) </code></pre> <p><img src="https://i.stack.imgur.com/IBQmG.png" alt="enter image description here"></p> <p>See also @mnel's answer <a href="https://stackoverflow.com/questions/19018568/possible-to-combine-position-jitter-with-position-dodge/19019810#comment28113777_19019810">here</a> for replacing values in the plot object. It might be worth trying here as well. You may also check <code>gtable</code> methods for arranging grobs. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload