简体   繁体   English

R (ggplot) - 是否可以将 X 或 Y 轴的“部分”加粗?

[英]R (ggplot) - Is it possible to bold 'part' of the X or Y axis.title?

I thought this would be simple, but can't seem to find anything.我以为这很简单,但似乎找不到任何东西。

I have a ggplot, which contains a label:我有一个 ggplot,其中包含一个标签:

labs(y = "Total Yearly Funding (£m) - 2019 Prices\n\n(log10)\n")

I want to bold everything but '(log10)' .我想加粗除'(log10)'

I thought it would be as simple as using '<b>' tags ie:我认为这就像使用'<b>'标签一样简单,即:

labs(y = "<b>Total Yearly Funding (£m) - 2019 Prices</b>\n\n(log10)\n")


But that doesn't do anything.但这没有任何作用。 Is what I want possible?我想要的可能吗?

Many thanks非常感谢

One method is to use an expression, another keyword could be italic if wanted:一种方法是使用表达式,如果需要,另一个关键字可以是italic

p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
p + labs(x=(expression(paste(bold("this is bold"), " and this is not bold"))))

第一个例子的情节


Edit: since we found another solution by commentary I want to input it here.编辑:因为我们通过评论找到了另一个解决方案,所以我想在这里输入它。 A simple line break is possible by using atop .使用atop可以实现简单的换行。 For further information check this old question .有关更多信息,请查看这个旧问题 atop provides one line break for the plotmath expressions and centers them. atopplotmath表达式提供一个换行符并将它们居中。

with:和:

labs(x=(expression(atop(bold("top"), "bottom"))))

we get:我们得到:

再试一次

How about this:这个怎么样:

p <- ggplot(...) + ggtitle("Yeah Buddy") +
  xlab("bold_x") + ylab("Total Yearly Funding (£m) - 2019 Prices\n\n(log10)\n")
p + theme(
axis.title.y = element_text(face="bold")
)
p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM