英文:
\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()
英文:
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 <- 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()
I currently have:
答案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("your_text_here"))))
<!-- -->"
英文:
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("your_text_here")))))
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论