Is there any possible way to change the labels for the facet_wrap variable, as displayed below. So, for example, instead of cyl: 4
, cyl: 6
, cyl: 8
, I want it to read condition: 4
, condition: 6
, condition: 8
. Of course, I can just do this by renaming the variable, but that's not what I want. This is a much simpler version of a custom function where I can't just rename a variable anyway I like.
Another way to put this is, do I have any freedom to label the facet_wrap
anyway I like? Kinda like how x
aesthetic variable in ggplot2
can have some name (e.g. cyl
) in the dataframe (mtcars
), but I can still replace it with my own name using labs(x = "cylinder")
). I want to have something similar for facet_wrap
.
library(dplyr)
library(datasets)
library(ggplot2)
data(mtcars)
# creating a dataframe
df <- dplyr::group_by(mtcars, .dots = c('cyl', 'am')) %>%
dplyr::summarize(counts = n()) %>%
dplyr::mutate(perc = (counts / sum(counts)) * 100) %>%
dplyr::arrange(desc(perc))
# preparing the plot
ggplot2::ggplot(df, aes('', counts)) +
geom_col(
position = 'fill',
color = 'black',
width = 1,
aes(fill = factor(am))
) +
facet_wrap(~cyl, labeller = "label_both") + # faceting by `cyl` variable
geom_label(
aes(label = paste0(round(perc), "%"), group = factor(am)),
position = position_fill(vjust = 0.5),
color = 'black',
size = 5,
show.legend = FALSE
) +
coord_polar(theta = "y")
Created on 2018-02-19 by the reprex package (v0.2.0).