[英]Trying to use a value in the ggplots title
I have been trying to put the value "Site 1" in a title for a ggplot, but I can't work out the syntax.我一直在尝试将值“站点 1”放在 ggplot 的标题中,但我无法弄清楚语法。
I have a tibble called g
:我有一个名为g
的小标题:
A tibble: 1 × 2
Site n
<chr> <int>
1 Site 1 27
And using this code:并使用此代码:
ggplot(g,aes(Site, n), fill=as.factor(Site))+
geom_col(aes(fill = Site), show.legend = FALSE) +
geom_text(aes(label = n), size = 3)+
labs(title=glue("adfadf", .$Site))
To create this plot:要创建此 plot:
enter image description here在此处输入图像描述
I want the title to say "This is a plot for Site 1", but I am doing something wrong.我想让标题说“这是站点 1 的 plot”,但我做错了。 I have tried different things other than the above, but I get errors like除了上述之外,我尝试了不同的东西,但我得到了类似的错误
"Error in force(..2): object '.' “有效错误(..2):object '。' not found"未找到”
for my version of the code.对于我的代码版本。 What is wrong in the title syntax?标题语法有什么问题?
You can just use ggtitle
to add the title to your plot.您可以使用ggtitle
将标题添加到 plot。
library(ggplot2)
ggplot(g, aes(Site, n), fill = as.factor(Site)) +
geom_col(aes(fill = Site), show.legend = FALSE) +
geom_text(aes(label = n), size = 3) +
ggtitle("This is a plot for Site 1")
Output Output
Or you can paste
the value to the phrase in ggtitle
:或者您可以将值paste
到ggtitle
中的短语中:
ggplot(g, aes(Site, n), fill = as.factor(Site)) +
geom_col(aes(fill = Site), show.legend = FALSE) +
geom_text(aes(label = n), size = 3) +
ggtitle(paste0("This is a plot for ", g$Site[1]))
Data数据
g <- structure(list(Site = "Site 1", n = 27L), class = "data.frame", row.names = "1")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.