代码之家  ›  专栏  ›  技术社区  ›  John-Henry

左对齐多行ggplot2图例

  •  0
  • John-Henry  · 技术社区  · 3 年前

    library(dplyr)
    library(ggplot2)
    mtcars %>% 
      mutate(qsec = factor(qsec)) %>% 
      ggplot(aes(mpg, cyl, fill = qsec, linetype = 'short legend')) +
      geom_col() +
      guides(fill = guide_legend(title.position = 'top', label.position = 'bottom'),
             linetype = guide_legend(title.position = 'top', 
                                      label.position = 'bottom')) +
      theme(legend.position = 'bottom',
            legend.box = 'vertical')
    

    linetype 图例是否与填充图例匹配?

    于2021年1月21日由 reprex package (v0.3.0)

    0 回复  |  直到 3 年前
        1
  •  1
  •   Ian Campbell xcodesucks123    3 年前

    也许是 legend.box.just = "left" 参数是您要查找的吗?

    mtcars %>% 
      mutate(qsec = factor(qsec)) %>% 
      ggplot(aes(mpg, cyl, fill = qsec, linetype = 'short legend')) +
      geom_col() +
      guides(fill = guide_legend(title.position = 'top', label.position = 'bottom'),
             linetype = guide_legend(title.position = 'top', 
                                     label.position = 'bottom')) +
      theme(legend.position = 'bottom', 
            legend.box.just = "left",
            legend.box = "vertical" )
    

    enter image description here