我正在尝试为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
我想成为所有ylabs中2nd
的大胆面孔。
然而,我明白了:
刻度标签应如下所示([=====]表示图中的条形图):
第1次第2次第3次[================================]
第一第二第三[=============================]
第一第二第三[==========================]
...
答案 0 :(得分:2)
您必须定义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"))) ))
stackoverflow上的帖子让我走上正轨: 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) )
答案 1 :(得分:0)
您需要更改axis.text.x,而不是直接在scale.x.discrete上工作。
作为创建labs
向量的旁注,您可以使用:
labs <- rep( c("1st","2nd","3rd"), 40)
font.faces <- rep( c("plain","bold","plain"), 40 )
大胆/朴素的面孔可以这样做:
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()