Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7776861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:59:11+00:00 2026-06-01T17:59:11+00:00

I stumbled onto this weird behavior with ggplot2s ordering of legends and just can’t

  • 0

I stumbled onto this weird behavior with ggplot2s ordering of legends and just can’t figure out what the logic behind the automatic placement of the legends is:

My aim: In a plot with multiple scales I want to arrange them in a different (thematic) order than the automatic one. But I couldn’t find a command in opts() or guides() to do this for me. And just to be clear: I don’t want to change the items within the legends, that works fine, but the arrangement of multiple complete legends.

So first I assumed they were ordered by type, i.e. scale, colour etc. But that is not the case, their position changes (see below).

Alphabetical order? No.

    library(ggplot2) ## v0.9

    ## Scale_colour on top
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class) +
    scale_size(name = "A") + scale_colour_discrete(name = "B")

    ## Reverse names --> scale_colour on bottom
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class) +
    scale_size(name = "B") + scale_colour_discrete(name = "A")

    ## Change name B to C -->  scale_colour on bottom
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class) +
    scale_size(name = "C") + scale_colour_discrete(name = "A")

    ## Change name B to D -->  scale_colour on top
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class) +
    scale_size(name = "D") + scale_colour_discrete(name = "A")

Further positions of scale_colour (for exchanged scale_size name)

  • “E”: bottom
  • “F” – “L”: top
  • “M” – “N”: bottom

and it continues to appear on top an at the bottom.

Factorial order? No.

    ## From top to bottom: C - B - A
    fname <- factor(c("A","B","C"), levels = c("A","B","C"))
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl) +
    scale_size(name = fname[1]) + scale_colour_discrete(name = fname[2]) + scale_alpha(name=fname[3])

    ## From top to bottom: B - C - A
    fname <- factor(c("A","B","C"), levels = c("C","B","A"))
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl) +
    scale_size(name = fname[1]) + scale_colour_discrete(name = fname[2]) + scale_alpha(name=fname[3])

    ## From top to bottom: B - C - A
    fname <- factor(c("A","B","C"), levels = c("B","C","A"))
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl)+
    scale_size(name = fname[1]) + scale_colour_discrete(name = fname[2]) + scale_alpha(name=fname[3])

Length of title? No.
I’ll stop for now with example code, but that one also yielded fluctuating orders independent of character length.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:59:13+00:00Added an answer on June 1, 2026 at 5:59 pm

    As I mentioned in the comment above, there is no way to control and predict the position of legend box.
    I wasn’t aware of this problem. Thank you for making clear this.

    Maybe some people need to control the legend box, here I put a quick fix:

    # run this code before calling ggplot2 function
    guides_merge <- function(gdefs) {
      gdefs <- lapply(gdefs, function(g) { g$hash <- paste(g$order, g$hash, sep = "z"); g})
      tapply(gdefs, sapply(gdefs, function(g)g$hash), function(gs)Reduce(guide_merge, gs))
    }
    environment(guides_merge) <- environment(ggplot)
    assignInNamespace("guides_merge", guides_merge, pos = "package:ggplot2")
    

    and then you can use order argument for guide_legend (and also guide_colorbar),

    # specify the order of the legend.
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl)+
     guides(size = guide_legend(order = 1), colour = guide_legend(order = 2), alpha = guide_legend(order = 3))
    
    qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl)+
     guides(size = guide_legend(order = 3), colour = guide_legend(order = 1), alpha = guide_legend(order = 2))
    

    order argument should be a positive integer. The legends are arranged along the order.
    Note that this is a quick fix so the interface may be changed in the next official version of ggplot2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i just stumbled onto this comment. public static int lowestOneBit(int i) { // HD,
I just stumbled onto this SO question and was wondering if there would be
just stumbled onto a miss in my code, a function was called like this
I stumbled across this C code today. Can anyone tell me what the 'where'
While l was looking over some questions about MEF, I stumbled onto this particular
I was just browsing some questions when I stumbled upon this one . I
Just stumbled into a weird thing with %ERRORLEVEL% and wanted to see if anyone
I stumbled onto this neat shortcut for converting a DOM NodeList into a regular
I've stumbled onto a very strange bug. Read the comments in the code to
I was looking through the docs and stumbled onto mysql_real_escape_string() and I'm not understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.