改编自这个问题和解决方案的问题:使用 ggplot2 以粗体突出显示各个轴标签
我想根据满足标准有选择地证明水平轴标签的合理性。因此,借用上述问题和答案,我设置了一个示例:
require(ggplot2)
require(dplyr)
set.seed(36)
xx<-data.frame(YEAR=rep(c("X", "Y"), each=20),
CLONE=rep(c("A", "B", "C", "D", "E"), each=4, 2),
TREAT=rep(c("T1", "T2", "T3", "C"), 10),
VALUE=sample(c(1:10), 40, replace=T))
# Simple plot with factors on y axis
ggplot(xx, aes(x = VALUE, y=CLONE, fill=YEAR)) +
geom_bar(stat="identity", position="dodge") +
facet_wrap(~TREAT)
![enter image description here](https://i.stack.imgur.com/PtVBb.jpg)
好吧,我采用了上面问题+答案中的函数来生成理由向量:
# Modify to control justification
colorado2 <- function(src, boulder) {
if (!is.factor(src)) src <- factor(src)
src_levels <- levels(src)
brave <- boulder %in% src_levels
if (all(brave)) {
b_pos <- purrr::map_int(boulder, ~which(.==src_levels))
b_vec <- rep(0.2, length(src_levels))
b_vec[b_pos] <- 0.9
b_vec
} else {
stop("All elements of 'boulder' must be in src")
}
}
# Redraw the plot with modifcation
ggplot(xx, aes(x = VALUE, y=CLONE, fill=YEAR)) +
geom_bar(stat="identity", position="dodge") +
facet_wrap(~TREAT) +
theme(axis.text.y=element_text(hjust=colorado2(xx$CLONE, c("A", "B", "E"))))
I'm getting this unfortunate mess:
![enter image description here](https://i.stack.imgur.com/XZHh4.jpg)
这些标签在我想要的方向上是合理的——但由于我无法弄清楚的原因,占据了太多的情节。我该如何解决 ?