ggplot2在使用两行时的坐标轴标签间隔

huangapple go评论121阅读模式
英文:

ggplot2 axis label spacing when using two rows

问题

当我运行以下的ggplot2代码时,我的坐标轴标签出现了一些奇怪的间距(例如,在第二行,就好像ggplot2试图将单位居中对齐,但然后将括号留得很远)。

  1. x <- c("x", "y")
  2. y <- c(5, 10)
  3. df <- data.frame(x, y)
  4. ggplot(df, aes(x = x, y = y)) +
  5. ylab(bquote('平均订阅者\n( ' * km^2 * ' )'))

有没有办法去掉这个空格并将坐标轴标签居中?

英文:

When I run the following ggplot2 code, I get some odd spacing in my axis label (e.g., on the second row, it's as if ggplot2 tries to center the units, but then leaves the parentheses very far apart).

  1. x &lt;- c(&quot;x&quot;, &quot;y&quot;)
  2. y &lt;- c(5, 10)
  3. df &lt;- data.frame(x, y)
  4. ggplot(df, aes(x = x, y = y)) +
  5. ylab(bquote(&#39;Mean Subscribers\n(&#39;*km^2*&#39;)&#39;))

ggplot2在使用两行时的坐标轴标签间隔

Any idea how I remove the space and center the axis label?
1: https://i.stack.imgur.com/0fr7R.png

答案1

得分: 1

另一个使用 bquote() 的替代方案是使用 {ggtext},它允许您使用 Markdown 语法。我发现它更直观,对多行标签的支持也更好。例如:

  1. library(ggtext)
  2. ggplot(df, aes(x = x, y = y)) +
  3. ylab('平均订阅者<br>(km²)') +
  4. theme(axis.title.y = element_markdown())

ggplot2在使用两行时的坐标轴标签间隔

英文:

An alternative solution to using bquote() is to use {ggtext} which lets you use markdown syntax. I've found it much more straightforward to use, and much better at multi-line labels. For example:

  1. library(ggtext)
  2. ggplot(df, aes(x = x, y = y)) +
  3. ylab(&#39;Mean Subscribers&lt;br&gt;(km&lt;sup&gt;2&lt;/sup&gt;)&#39;) +
  4. theme(axis.title.y = element_markdown())

ggplot2在使用两行时的坐标轴标签间隔

huangapple
  • 本文由 发表于 2023年7月3日 22:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定