我想使用函数标记,但是我收到错误。非常感谢您的投入!
library(ggplot2)
drink <- c(replicate(18, "Water"),
replicate(22, "Beer"),
replicate(20, "Coke"))
person <- c(replicate(6, c(replicate(5, 1), replicate(5, 2))))
dd <- data.frame(person, drink)
rm(drink, person)
mf_labeller <- function(var, value){
value <- as.character(value)
if(var == "person"){
value[value == "1"] <- "Women"
value[value == "2"] <- "Men"
}
return(value)
}
p <- ggplot(dd,
aes(drink)) +
geom_bar(stat = "count") +
facet_grid(person ~ .,
labeller = mf_labeller)
p
答案 0 :(得分:1)
也许有一种更简单的方法,而不是使用函数:
person_values <- c("1" = "Women",
"2" = "Men")
ggplot(dd, aes(drink)) +
geom_bar(stat = "count") +
facet_grid(person ~ ., labeller = as_labeller(person_values))
等同于labeller
文档:
ggplot(dd, aes(drink)) +
geom_bar(stat = "count") +
facet_grid(person ~ ., labeller = labeller(person = person_values))
请注意,此解决方案来自a previously asked question。