【发布时间】:2018-01-16 16:49:51
【问题描述】:
问题改编自这个问题和解决方案:Highlighting individual axis labels in bold using 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)
好的,所以我采用上述问题+答案中的函数来生成理由向量:
# 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"))))
标签在我想要的方向上是合理的 - 但由于我无法弄清楚的原因占据了太多的情节。我该如何解决这个问题?
【问题讨论】: