在ggplot2中动态格式化各个轴标签

时间:2023-01-27 20:24:31

This may end up being an expression or call question, but I am trying to conditionally format individual axis labels.

这可能最终成为表达式或调用问题,但我试图有条件地格式化单个轴标签。

In the following example, I'd like to selectively bold one of the axis labels:

在下面的示例中,我想有选择地加粗其中一个轴标签:

library(ggplot2)

data <- data.frame(labs = c("Oranges", "Apples", "Cucumbers"), counts = c(5, 10, 12))

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity")`

在ggplot2中动态格式化各个轴标签

There is similar problem here, but the solution involves theme and element_text. I am trying to use axis labels directly.

这里有类似的问题,但解决方案涉及theme和element_text。我试图直接使用轴标签。

I can do this manually as below:

我可以手动执行此操作,如下所示:

breaks <- levels(data$labs)
labels <- breaks
labels[2] <- expression(bold("Cucumbers"))

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity") + 
  scale_x_discrete(label = labels, breaks = breaks)

在ggplot2中动态格式化各个轴标签

But, if I try to do it by indexing instead of typing out "Cucumbers", I get the following error:

但是,如果我尝试通过索引而不是输入“Cucumbers”来做到这一点,我会收到以下错误:

breaks <- levels(data$labs)
labels <- breaks
labels[2] <- expression(bold(labels[2]))

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity") + 
  scale_x_discrete(label = labels, breaks = breaks)

在ggplot2中动态格式化各个轴标签

Which makes sense, because it is not evaluating the labels[2]. But, does anyone know how to force it to do that? Thanks.

这是有道理的,因为它没有评估标签[2]。但是,有谁知道如何强迫它这样做?谢谢。

2 个解决方案

#1


11  

How about

怎么样

breaks <- levels(data$labs)
labels <- as.expression(breaks)
labels[[2]] <- bquote(bold(.(labels[[2]])))

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity") + 
  scale_x_discrete(label = labels, breaks = breaks)

Here we are more explicit about the conversion to expression and we use bquote() to insert the value of the label into the expression itself.

这里我们更明确地转换为表达式,我们使用bquote()将标签的值插入表达式本身。

#2


2  

Another option is to set the font face dynamically with theme, though I'm not sure if this is in any sense a better or worse method than @MrFlick's answer:

另一种选择是使用主题动态设置字体,但我不确定这是否比@ MrFlick的答案更好或更差:

breaks <- levels(data$labs)

# Reference breaks by name
toBold = "Cucumbers"

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity") + 
  scale_x_discrete(label = labels, breaks = breaks) +
  theme(axis.text.x=
        element_text(face=ifelse(breaks %in% toBold, "bold", "plain")))

# Reference breaks by position
label.index=2

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity") + 
  scale_x_discrete(label = labels, breaks = breaks) +
  theme(axis.text.x=
        element_text(face=ifelse(breaks %in% labels[match(label.index, 1:length(breaks))], 
                                 "bold", "plain")))

#1


11  

How about

怎么样

breaks <- levels(data$labs)
labels <- as.expression(breaks)
labels[[2]] <- bquote(bold(.(labels[[2]])))

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity") + 
  scale_x_discrete(label = labels, breaks = breaks)

Here we are more explicit about the conversion to expression and we use bquote() to insert the value of the label into the expression itself.

这里我们更明确地转换为表达式,我们使用bquote()将标签的值插入表达式本身。

#2


2  

Another option is to set the font face dynamically with theme, though I'm not sure if this is in any sense a better or worse method than @MrFlick's answer:

另一种选择是使用主题动态设置字体,但我不确定这是否比@ MrFlick的答案更好或更差:

breaks <- levels(data$labs)

# Reference breaks by name
toBold = "Cucumbers"

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity") + 
  scale_x_discrete(label = labels, breaks = breaks) +
  theme(axis.text.x=
        element_text(face=ifelse(breaks %in% toBold, "bold", "plain")))

# Reference breaks by position
label.index=2

ggplot(data = data) + 
  geom_bar(aes(x = labs, y = counts), stat="identity") + 
  scale_x_discrete(label = labels, breaks = breaks) +
  theme(axis.text.x=
        element_text(face=ifelse(breaks %in% labels[match(label.index, 1:length(breaks))], 
                                 "bold", "plain")))