将文本移到下方,使用哪种语法使文本变小?(ggplot)

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

\n to move text below, which syntax to make text smaller? (ggplot)

问题

我创建以下图表,并根据以下内容创建每个变量的标签。我可以包括 \n 并将文本移至下面,就像下面的示例一样,但如何使文本 "Cat.1 [1-3] & Cat.2 [3-5]" 变小?我尝试使用 \small 但不起作用。

graph <- within(results, Variable_label[Variable=="Var_1"] <- "Average age of individuals \n Cat.1 [1-3] & Cat.2 [3-5]")
    
graph %>%
  ggplot(aes(year, Variable_label, fill = other_variable)) +
  geom_tile(color = "white") +
  coord_equal()

我目前的效果如下:将文本移到下方,使用哪种语法使文本变小?(ggplot)

英文:

I am creating the following graph with labels being created for each variable as per the below. I can include \n and move the text below the as per the example below, but how can I make the text "Cat.1 [1-3] & Cat.2 [3-5]" smaller? I tried \small but didn't work.

graph &lt;- within(results, Variable_label[Variable==&quot;Var_1&quot;] &lt;- &quot;Average age of individuals \n Cat.1 [1-3] &amp; Cat.2 [3-5]&quot;)

graph %&gt;% 
  ggplot(aes(year, Variable_label, fill = other_variable)) +
  geom_tile(color = &quot;white&quot;) +
  coord_equal()

I currently have:

将文本移到下方,使用哪种语法使文本变小?(ggplot)

答案1

得分: 2

这是你要的翻译:

"在ggplots中放置带有换行的格式有点困难。这是一个使用鸢尾花数据集的示例(请分享一些数据,以便我们能够重现您的绘图)。关键在于R中的表达式是由plotmath解析的(参见?plotmath)。在这里,我使用了atop函数(这是一个plotmath函数,不是一个真正的函数)将文本分成两行,并使用scriptsyle来更改大小。另一种方法是使用annotate注释。

library(ggplot2)
data(iris)

ggplot(iris, aes(Sepal.Width, Species, color = Sepal.Length)) + geom_tile() + 
  scale_y_discrete(labels = \(x) lapply(x, \(x) bquote(atop(.(x),  scriptstyle(&quot;your_text_here&quot;))))

将文本移到下方,使用哪种语法使文本变小?(ggplot)<!-- -->"

英文:

It is somewhat hard to put format with line breaks in ggplots.

This is an example made with iris (please share some data so we can reproduce your plot). The key is that expressions in r are parsed by plotmath (see ?plotmath).
Here i use the function atop (which is a plotmath function not a real function) to put the text in two lines, and scriptsyle to change size.

Other way is using annotate annotations.

library(ggplot2)
data(iris)

ggplot(iris, aes(Sepal.Width, Species, color = Sepal.Length)) + geom_tile() + 
  scale_y_discrete(labels = \(x) lapply(x, \(x) bquote(atop(.(x),  scriptstyle(&quot;your_text_here&quot;)))))

将文本移到下方,使用哪种语法使文本变小?(ggplot)<!-- -->

huangapple
  • 本文由 发表于 2023年3月4日 02:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630471.html
匿名

发表评论

匿名网友

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

确定