r_eda

legend_guide 改变图例摆放方式

李家翔 2019-03-21

suppressMessages(library(tidyverse))
args(guide_legend)
## function (title = waiver(), title.position = NULL, title.theme = NULL, 
##     title.hjust = NULL, title.vjust = NULL, label = TRUE, label.position = NULL, 
##     label.theme = NULL, label.hjust = NULL, label.vjust = NULL, 
##     keywidth = NULL, keyheight = NULL, direction = NULL, default.unit = "line", 
##     override.aes = list(), nrow = NULL, ncol = NULL, byrow = FALSE, 
##     reverse = FALSE, order = 0, ...) 
## NULL
mtcars %>% 
    rownames_to_column('name') %>% 
    head(6) %>% 
    ggplot(aes(x = name, y = mpg, fill = name)) +
    geom_col() +
    coord_flip() +
    theme(
        legend.position = 'bottom'
    ) +
    guides(
        fill = guide_legend(
            ncol = 1
        )
    )

参考 Stack Overflow guides -> guide_legend 改变了图例的摆放方式。

有时候 colfill 都要用