How to specify columns in facet_grid OR how to change labels in facet_wrap

前端 未结 4 562
难免孤独
难免孤独 2020-12-02 15:22

I have a large number of data series that I want to plot using small multiples. A combination of ggplot2 and facet_wrap does what I want, typically resulting a

相关标签:
4条回答
  • 2020-12-02 16:07

    Just add labeller = label_wrap_gen(width = 25, multi_line = TRUE) to the facet_wrap() arguments.

    Eg.: ... + facet_wrap( ~ variable, ,labeller = label_wrap_gen(width = 25, multi_line = TRUE))

    More info: ?ggplot2::label_wrap_gen

    0 讨论(0)
  • 2020-12-02 16:09

    I don't quite understand. You've already written a function that converts your short labels to long, descriptive labels. What is wrong with simply adding a new column and using facet_wrap on that column instead?

    mydf <- melt(mydf, id = c('date'))
    mydf$variableLab <- mf_labeller('variable',mydf$variable)
    
    p1 <- ggplot(mydf, aes(y = value, x = date, group = variable)) +
        geom_line() +
        facet_wrap( ~ variableLab, ncol = 2)
    print (p1)
    
    0 讨论(0)
  • 2020-12-02 16:13

    To change the label names, just change the factor levels of the factor you use in facet_wrap. These will be used in facet_wrap on the strips. You can use a similar setup as you would using the labeller function in facet_grid. Just do something like:

    new_labels = sapply(levels(df$factor_variable), custom_labeller_function)
    df$factor_variable = factor(df$factor_variable, levels = new_labels)
    

    Now you can use factor_variable in facet_wrap.

    0 讨论(0)
  • 2020-12-02 16:14

    Simply add labeller = label_both to the facet_wrap() arguments.

    ... + facet_wrap( ~ variable, labeller = label_both)
    
    0 讨论(0)
提交回复
热议问题