How to change facet labels?

后端 未结 20 1642
既然无缘
既然无缘 2020-11-22 14:50

I have used the following ggplot command:

ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ..count.. / n), binwidth = 10)
  + scale         


        
相关标签:
20条回答
  • 2020-11-22 15:28

    I feel like I should add my answer to this because it took me quite long to make this work:

    This answer is for you if:

    • you do not want to edit your original data
    • if you need expressions (bquote) in your labels and
    • if you want the flexibility of a separate labelling name-vector

    I basically put the labels in a named vector so labels would not get confused or switched. The labeller expression could probably be simpler, but this at least works (improvements are very welcome). Note the ` (back quotes) to protect the facet-factor.

    n <- 10
    x <- seq(0, 300, length.out = n)
    
    # I have my data in a "long" format
    my_data <- data.frame(
      Type = as.factor(c(rep('dl/l', n), rep('alpha', n))),
      T = c(x, x),
      Value = c(x*0.1, sqrt(x))
    )
    
    # the label names as a named vector
    type_names <- c(
      `nonsense` = "this is just here because it looks good",
      `dl/l` = Linear~Expansion~~Delta*L/L[Ref]~"="~"[%]", # bquote expression
      `alpha` = Linear~Expansion~Coefficient~~alpha~"="~"[1/K]"
      )
    
    
    ggplot() + 
      geom_point(data = my_data, mapping = aes(T, Value)) + 
      facet_wrap(. ~ Type, scales="free_y", 
                 labeller = label_bquote(.(as.expression(
                   eval(parse(text = paste0('type_names', '$`', Type, '`')))
                   )))) +
      labs(x="Temperature [K]", y="", colour = "") +
      theme(legend.position = 'none')
    

    0 讨论(0)
  • 2020-11-22 15:29

    Here's how I did it with facet_grid(yfacet~xfacet) using ggplot2, version 2.2.1:

    facet_grid(
        yfacet~xfacet,
        labeller = labeller(
            yfacet = c(`0` = "an y label", `1` = "another y label"),
            xfacet = c(`10` = "an x label", `20` = "another x label")
        )
    )
    

    Note that this does not contain a call to as_labeller() -- something that I struggled with for a while.

    This approach is inspired by the last example on the help page Coerce to labeller function.

    0 讨论(0)
  • 2020-11-22 15:29

    Both facet_wrap and facet_grid also accept input from ifelse as an argument. So if the variable used for faceting is logical, the solution is very simple:

    facet_wrap(~ifelse(variable, "Label if true", "Label if false"))
    

    If the variable has more categories, the ifelse statement needs to be nested.

    As a side effect, this also allows the creation of the groups to be faceted within the ggplot call.

    0 讨论(0)
  • 2020-11-22 15:29

    Have you tried changing the specific levels of your Hospital vector?

    levels(survey$hospital)[levels(survey$hospital) == "Hospital #1"] <- "Hosp 1"
    levels(survey$hospital)[levels(survey$hospital) == "Hospital #2"] <- "Hosp 2"
    levels(survey$hospital)[levels(survey$hospital) == "Hospital #3"] <- "Hosp 3"
    
    0 讨论(0)
  • 2020-11-22 15:30

    I think all other solutions are really helpful to do this, but there is yet another way.

    I assume:

    • you have installed the dplyr package, which has the convenient mutate command, and
    • your dataset is named survey.

      survey %>% mutate(Hosp1 = Hospital1, Hosp2 = Hospital2,........)

    This command helps you to rename columns, yet all other columns are kept.

    Then do the same facet_wrap, you are fine now.

    0 讨论(0)
  • 2020-11-22 15:35

    The EASIEST way to change WITHOUT modifying the underlying data is:

    1. Create an object using the as_labeller function adding the back tick mark for each of the default values:

      hum.names <- as_labeller(c(50 = "RH% 50", 60 = "RH% 60",70 = "RH% 70", 80 = "RH% 80",90 = "RH% 90", 100 = "RH% 100")) #Necesarry to put RH% into the facet labels

    2. We add into the GGplot:

      ggplot(dataframe, aes(x=Temperature.C,y=fit))+geom_line()+ facet_wrap(~Humidity.RH., nrow=2,labeller=hum.names)

    0 讨论(0)
提交回复
热议问题