r - How to add labels to plot with facets? - TagMerge
3How to add labels to plot with facets?How to add labels to plot with facets?

How to add labels to plot with facets?

Asked 1 years ago
3 answers

When adding labels to facet plots, I've found that it's usually easier to compute what you want ahead of time:

food_labels <- food_totals %>% 
  group_by(food) %>% 
  summarize(x = mean(year), label = unique(overall_change))

  food         x label
  <chr>    <dbl> <dbl>
1 Eggplant  2013    -4
2 Kiwi      2013     2
3 Orange    2013    -3

We can then plot this using the average year per facet to center the text ("x" in the data frame above), and Inf for the y-value to ensure that labels always appear at the top of the plot.

food_totals %>% 
  ggplot(aes(year, total)) + 
  geom_line(aes(colour = food, group = food)) + 
  geom_text(data = food_labels, aes(x = x, label = label), y = Inf, vjust = 2) +
  facet_wrap(vars(food)) + 
  theme(axis.text.x = element_text(angle = 90))

enter image description here

Source: link


I am attempting to add labels (capital letters) to each plot in the following facet_grid:
p <- ggplot(mpg, aes(displ, cty)) + geom_point()
p + facet_grid(drv ~ cyl)
How about this? Fixing the position of label as the upper-left corner of each plot panel:
p + facet_grid(drv ~ cyl)+ annotate('text', label = LETTERS[1:12], x=min(mpg$displ), y=max(mpg$cty))
You can also fine-tune the position of the label proportional to both axis by using:
x=mean(mpg$displ)*0.6, y=max(mpg$cty)*0.97

Source: link


Load required packages and set the theme function theme_light() [ggplot2] as the default theme:
  theme_light() + theme(legend.position = "top")
Create a box plot filled by groups:
# Load data and convert dose to a factor variable
ToothGrowth$dose <- as.factor(ToothGrowth$dose)

# Box plot, facet accordding to the variable dose and supp
p <- ggplot(ToothGrowth, aes(x = dose, y = len)) + 
  geom_boxplot(aes(fill = supp), position = position_dodge(0.9)) +
p + facet_grid(dose ~ supp)
In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.
p + facet_grid(dose ~ supp, labeller = label_both)
A simple way to modify facet label text, is to provide new labels as a named character vector:
# New facet label names for dose variable
dose.labs <- c("D0.5", "D1", "D2")
names(dose.labs) <- c("0.5", "1", "2")

# New facet label names for supp variable
supp.labs <- c("Orange Juice", "Vitamin C")
names(supp.labs) <- c("OJ", "VC")

# Create the plot
p + facet_grid(
  dose ~ supp, 
  labeller = labeller(dose = dose.labs, supp = supp.labs)
An alternative solution to change the facet labels, is to modify the data:
df <- ToothGrowth
# Modify the data
df$dose <- factor(df$dose, levels = c("0.5", "1", "2"), 
                  labels = c("D0.5", "D1", "D2"))
df$supp <- factor(df$supp, levels = c("OJ", "VC"),
                  labels = c("Orange Juice", "Vitamin C")
# Create the plot
ggplot(df, aes(x = dose, y = len)) + 
  geom_boxplot(aes(fill = supp)) +
  facet_grid(dose ~ supp)

Source: link

Recent Questions on r

    Programming Languages