ggplot 标签中的两个字体

Two font faces in ggplot labels

我正在尝试为 y 标签创建一个具有两个字体的条形图(不要混淆:由于 coord_flip(),它被称为 x)。 但是,我还没有在互联网上找到任何解决方案。

有可能吗?

到目前为止我已经知道了。

library(ggplot2)

labs <- paste(rep("1st", 40), rep("2nd", 40), rep("3rd", 40))
labs <- strsplit(labs, " ")

v1 <- ggplot(data.frame(x = 1:40, y = 1:40), aes(x,y)) + 
  geom_bar(stat = "identity", fill = "grey50") + 
  coord_flip() + 
  scale_x_discrete(labels = paste(sapply(labs, "[[", 1), 
                                  .(bold(.(sapply(labs, "[[", 2)))),
                                  sapply(labs, "[[", 3)), 
                   breaks = 1:40)
v1

我想成为所有 ylab 中的 2nd 粗体字。 然而,我明白了:

刻度标签应如下所示([=====] 表示图中的条形):

第一 第二 第三 [=============================== =]

第一 第二 第三 [=============================]

第一 第二 第三 [==========================]

...

一般

您需要更改 axis.text.x 而不是直接在 scale.x.discrete 上工作。

作为创建 labs 向量的旁注,您可以使用:

labs <- rep( c("1st","2nd","3rd"), 40)
font.faces <- rep( c("plain","bold","plain"), 40 )

bold/plain脸可以这样画:

library(ggplot2)

# have some example data
all.data <- data.frame(group=c('a','b'),count=c(5,8))

ggplot(data=all.data, aes(group, count)) + 
 geom_bar(stat = "identity") + 
 # put your names here
 scale_x_discrete(label=c("foo","bar")) + 
 # put a vector assigning bold and plain here
 # e.g. you can put font.faces vector here if you have 40 labels
 theme( axis.text.x = element_text(face=c("bold","plain")) )

例子

一个更明确的例子就是这个。请确保您的 ggplot 语句中有关元素数量不等的错误不是由于您在 x 轴(翻转后的 y 轴)上的样本命名是重复的。它们可能需要是唯一的。

# generate some data
all.data <- as.data.frame(matrix(seq(30), nrow = 30, ncol = 1))

# produce labels and font faces
# labels need to be unique to make sure ggplot2 doesn't summarise the categories
labs <- rep( c("1st","2nd","3rd"), 10)
labs.ext <- paste(labs, rep(seq(10), each=3), sep='_')
font.faces <- rep( c("plain", "bold", "plain"), 10)

# add a label column to the data frame (and make sure that it will be a  factor with levels in your intended order)
all.data[,2] <- factor(labs.ext, levels = labs.ext)
colnames(all.data) <- c("count","name")

# including your coord_flip(), x axis transforms to y axis
ggplot(data=all.data, aes(name, count)) + geom_bar(stat='identity') + theme(axis.text.y = element_text(face = font.faces)) + coord_flip()

最小示例

您必须定义一个 expression

library(ggplot2)

all.data <- data.frame(group=c('a','b'),count=c(5,8))

ggplot(data=all.data, aes(group, count)) + 
  geom_bar(stat = "identity") + 
  scale_x_discrete(label=c( expression(paste("xxx",bold("foo"))), expression(paste("yyy",bold("bar")))  ))

使用变量的示例

Whosebug 上的 post 让我走上了正确的轨道: Use expression with a variable r

labs <- paste(rep("1st", 40), rep("2nd", 40), rep("3rd", 40))
labs <- strsplit(labs, " ")

# create a vector of expressions
# everything in .() is evaluated and ~ forms an expression
plot.labs <- bquote( .(labs[[1]][1]) ~ bold(.(labs[[1]][2])) ~ .(labs[[1]][3]))

# produce a list of expressions
plot.labs.apply <- lapply(labs, function(x) {plot.labs <- bquote( .(x[1]) ~ bold(.(x[2])) ~ .(x[3]))})

# was it done correctly?
class(plot.labs.apply[[2]])

# i used a smaller data frame to not overload the plot
ggplot(data.frame(x = c('a','b'), y = c(40,25)), aes(x,y)) + 
    geom_bar(stat = "identity", fill = "grey50") + 
    coord_flip() + 
    scale_x_discrete(label = c(plot.labs.apply) )