使用for循环将全局变量(这里是'var')放入ggplot的ggtitle中的粗体字符串中
大家好,我想使用 for 循环将全局变量(此处为“var”)放入 ggplot 的 ggtitle 中的粗体字符串中。
该图没有与“var”相对应的值,它只是显示为“var”。
a <- data.frame(date = 1:5, co2 = 11:15, year = 2021:2025)
var = 2021
for (var in 2021:2025) {
print(
a %>%
filter(year == var) %>%
ggplot(aes(date, co2)) +
geom_point() +
ggtitle(expression(paste(
bolditalic(var),
bolditalic("KOR CO2"))))
)
}
我希望图表标题为 2021 KOR CO2、2022 KOR CO2、...、< strong>2025 KOR CO2。
你能解决这个问题吗?
reference : How to use bold, italic and underline in ggplot2
Hi, I want to put the global variable(here is 'var') in the bolditalic string in ggtitle of ggplot using for loop.
The graph doesn't have a value corresponding to 'var', it just comes out as 'var'.
a <- data.frame(date = 1:5, co2 = 11:15, year = 2021:2025)
var = 2021
for (var in 2021:2025) {
print(
a %>%
filter(year == var) %>%
ggplot(aes(date, co2)) +
geom_point() +
ggtitle(expression(paste(
bolditalic(var),
bolditalic("KOR CO2"))))
)
}
I want graph titles to be 2021 KOR CO2, 2022 KOR CO2, ..., 2025 KOR CO2.
Can you please solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了 MrFlick 的解决方案之外:
ggplot
还提供通过一两个分组变量自动进行子集化和绘制数据的功能。您可以使用facet_wrap
和facet_grid
方便地实现此类分面图。在你的情况下:
In addition to MrFlick's solution:
ggplot
offers automated subsetting and plotting data by one or two grouping variables. You can achieve such facetted plots conveniently withfacet_wrap
andfacet_grid
.In your case: