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 1111593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:34:19+00:00 2026-05-17T02:34:19+00:00

Right now, the legend by default looks something like this: Legend Title x-1 y-2

  • 0

Right now, the legend by default looks something like this:

Legend Title
x-1 
y-2 
z-3 

But is it possible to make it look something like this?

Legend Title 
x-1 y-2 z-3
  • 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-05-17T02:34:20+00:00Added an answer on May 17, 2026 at 2:34 am

    and here is a hack.
    there may be bugs, though:

    build_legend <- function(name, mapping, layers, default_mapping, theme) {
      legend_data <- plyr::llply(layers, build_legend_data, mapping, default_mapping)
    
      # determine if the elements are aligned horizontally or vertically
      horiz<-(!is.null(theme$legend.align) && theme$legend.align=="horizontal")
    
      # Calculate sizes for keys - mainly for v. large points and lines
      size_mat <- do.call("cbind", plyr::llply(legend_data, "[[", "size"))
      if (is.null(size_mat)) {
        key_sizes <- rep(0, nrow(mapping))
      } else {
        key_sizes <- apply(size_mat, 1, max)
      }
    
      title <- theme_render(
        theme, "legend.title",
        name, x = 0, y = 0.5
      )
    
                                            # Compute heights and widths of legend table
      nkeys <- nrow(mapping)
      hgap <- vgap <- unit(0.3, "lines")
    
      numeric_labels <- all(sapply(mapping$.label, is.language)) || suppressWarnings(all(!is.na(sapply(mapping$.label, "as.numeric"))))
      hpos <- numeric_labels * 1
    
      labels <- lapply(mapping$.label, function(label) {
        theme_render(theme, "legend.text", label, hjust = hpos, x = hpos, y = 0.5)
      })
    
      # align horizontally
      if(!horiz){
        label_width <- do.call("max", lapply(labels, grobWidth))
        label_width <- convertWidth(label_width, "cm")
        label_heights <- do.call("unit.c", lapply(labels, grobHeight))
        label_heights <- convertHeight(label_heights, "cm")
    
        width <- max(unlist(plyr::llply(legend_data, "[[", "size")), 0)
        key_width <- max(theme$legend.key.size, unit(width, "mm"))
    
        widths <- unit.c(
                         hgap, key_width,
                         hgap, label_width,
                         max(
                             unit(1, "grobwidth", title) - key_width - label_width,
                             hgap
                             )
                         )
        widths <- convertWidth(widths, "cm")
    
        heights <- unit.c(
                          vgap, 
                          unit(1, "grobheight", title),
                          vgap, 
                          unit.pmax(
                                    theme$legend.key.size, 
                                    label_heights, 
                                    unit(key_sizes, "mm")
                                    ),
                          vgap
                          )  
        heights <- convertHeight(heights, "cm")
    
      }else{
        label_width <- do.call("unit.c", lapply(labels, grobWidth))
        label_width <- convertWidth(label_width, "cm")
        label_heights <- do.call("max", lapply(labels, grobHeight))
        label_heights <- convertHeight(label_heights, "cm")
    
        height <- max(unlist(plyr::llply(legend_data, "[[", "size")), 0)
        key_heights <- max(theme$legend.key.size, unit(height, "mm"))
    
        key_width <- unit.pmax(theme$legend.key.size, unit(key_sizes, "mm"))
        # width of (key gap label gap) x nkeys
        kglg_width<-do.call("unit.c",lapply(1:length(key_width), function(i)unit.c(key_width[i], hgap, label_width[i], hgap)))
        widths <- unit.c(
                          hgap,
                          kglg_width,
                          max(
                              unit(0,"lines"),
                              unit.c(unit(1, "grobwidth", title) - (sum(kglg_width) - hgap))
                              )
                          )
        widths <- convertWidth(widths, "cm")
    
        heights <- unit.c(
                           vgap, 
                           unit(1, "grobheight", title),
                           vgap, 
                           max(
                               theme$legend.key.size,
                               label_heights, 
                               key_heights
                               ),
                           vgap
                           )  
      heights <- convertHeight(heights, "cm")
    
      }
    
      # Layout the legend table
      legend.layout <- grid.layout(
        length(heights), length(widths), 
        widths = widths, heights = heights, 
        just = c("left", "centre")
      )
    
      fg <- ggname("legend", frameGrob(layout = legend.layout))
      fg <- placeGrob(fg, theme_render(theme, "legend.background"))
    
      fg <- placeGrob(fg, title, col = 2:(length(widths)-1), row = 2)
      for (i in 1:nkeys) {
    
        if(!horiz){
          fg <- placeGrob(fg, theme_render(theme, "legend.key"), col = 2, row = i+3)
        }else{
          fg <- placeGrob(fg, theme_render(theme, "legend.key"), col = 1+(i*4)-3, row = 4)
        }
    
        for(j in seq_along(layers)) {
          if (!is.null(legend_data[[j]])) {
            legend_geom <- Geom$find(layers[[j]]$geom$guide_geom())
            key <- legend_geom$draw_legend(legend_data[[j]][i, ],
               c(layers[[j]]$geom_params, layers[[j]]$stat_params))
            if(!horiz){
              fg <- placeGrob(fg, ggname("key", key), col = 2, row = i+3)
            }else{
              fg <- placeGrob(fg, ggname("key", key), col = 1+(i*4)-3, row = 4)
            }
          }
        }
        label <- theme_render(
          theme, "legend.text", 
          mapping$.label[[i]], hjust = hpos,
          x = hpos, y = 0.5
        )
        if(!horiz){
          fg <- placeGrob(fg, label, col = 4, row = i+3)
        }else{
          fg <- placeGrob(fg, label, col = 1+(i*4)-1, row = 4)
        }
      }
      fg
    }
    
    assignInNamespace("build_legend", build_legend, "ggplot2")
    
    # test and usage
    # specify by opts(legend.align="horizontal")
    p1<-qplot(mpg, wt, data=mtcars, colour=cyl)+opts(legend.align="horizontal",legend.position="bottom")
    p2<-qplot(mpg, wt, data=mtcars, colour=cyl)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I'm doing something like this: RewriteRule ^/?logout(/)?$ logout.php RewriteRule ^/?config(/)?$ config.php I
Right now my ant task looks like. <javadoc sourcepath=${source} destdir=${doc}> <link href=http://java.sun.com/j2se/1.5.0/docs/api/ /> </javadoc>
Right now when I run this it keeps clicking on the same button every
I am trying to right-align the entries in a matplotlib axes legend (by default
Right now, I have a domain entity named StyleBundle. This StyleBundle takes a list
right now i have this <script type='text/javascript'> $(#beau).click(function(){ $(#beau).animate({margin-Top: 738px}, fast); }); </script> and
Right now I'm learning Opencart, and I'm trying to make my latest product display
Right now, when I submit a form, I get this in my URL: http://www.taiwantalk.org/users/admin/edit/?updated=true
right now I'm creating an array and using: render :json => @comments This would
iam trying to achieve this exact functionalty: http://jsfiddle.net/exttq/ BUT inside using my PHP, right

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.