使用 ggplot2 以粗体突出显示单个轴标签 [英] Highlighting individual axis labels in bold using ggplot2

查看:61
本文介绍了使用 ggplot2 以粗体突出显示单个轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以粗体突出显示各个轴标签.我知道@MrFlick 的这个

现在我要加粗Clone ABE.我相信这会以某种方式起作用,但我不知道如何.理想情况下,通过a) 使用列表/表达式中的项目编号和 b) 使用标签来了解如何执行此操作会很棒,例如ABE.

解决方案

这是创建大胆向量的通用方法:

colorado <- function(src, boulder) {if (!is.factor(src)) src <- factor(src) # 确保它是一个因子src_levels <- levels(src) # 按级别检索级别勇敢的 <- boulder %in% src_levels # 确保我们想要加粗的所有内容实际上都在因子级别if (all(brave)) { # 如果是这样b_pos <- purrr::map_int(boulder, ~which(.==src_levels)) # 然后找出它们在哪里b_vec <- rep("plain", length(src_levels)) # 先让一切变得简单b_vec[b_pos] <- "bold" # 使我们的目标加粗b_vec # 返回新向量} 别的 {stop("'boulder' 的所有元素都必须在 src")}}ggplot(xx, aes(x=CLONE, y=VALUE, fill=YEAR)) +geom_bar(stat="identity", position="dodge") +facet_wrap(~TREAT) +主题(axis.text.x=element_text(face=colorado(xx$CLONE,c(A",B",E"))))

I want to highlight individual axis labels in bold. I am aware of this answer by @MrFlick but I can't figure out how to do this a) for more than one item, and b) whether it's possible to use the names of the labels instead of the number of the item in that list (or expression).

Here is an example dataset:

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))

Then I am sorting my labels according to a particular factor combination which is then supposed to be maintained across multiple panels of a plot. See my previous question here.

clone_order <- xx %>% subset(TREAT == "C"  & YEAR == "X") %>%
  arrange(-VALUE) %>% select(CLONE) %>% unlist()    
xx <- xx %>% mutate(CLONE = factor(CLONE, levels = clone_order))

ggplot(xx, aes(x=CLONE, y=VALUE, fill=YEAR)) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~TREAT)

Now I want to bold Clone A, B and E. I am sure this will work somehow but I cannot figure out how. Ideally, it would be great to know how to do this by a) using the number of the item in the list/expression, and b) by using the label, e.g. A, B and E.

解决方案

Here's a generic method to create the emboldening vector:

colorado <- function(src, boulder) {
  if (!is.factor(src)) src <- factor(src)                   # make sure it's a factor
  src_levels <- levels(src)                                 # retrieve the levels in their order
  brave <- boulder %in% src_levels                          # make sure everything we want to make bold is actually in the factor levels
  if (all(brave)) {                                         # if so
    b_pos <- purrr::map_int(boulder, ~which(.==src_levels)) # then find out where they are
    b_vec <- rep("plain", length(src_levels))               # make'm all plain first
    b_vec[b_pos] <- "bold"                                  # make our targets bold
    b_vec                                                   # return the new vector
  } else {
    stop("All elements of 'boulder' must be in src")
  }
}

ggplot(xx, aes(x=CLONE, y=VALUE, fill=YEAR)) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~TREAT) +
  theme(axis.text.x=element_text(face=colorado(xx$CLONE, c("A", "B", "E"))))

这篇关于使用 ggplot2 以粗体突出显示单个轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆