如何使用左对齐和右对齐文本为标题添加标题

问题描述 投票:-2回答:1

这是我想要实现的一个例子,但是使用PerformanceAnalytics

library(PerformanceAnalytics)

data(edhec)
chart.TimeSeries(edhec)

这给出了图表:Nice graph你可以看到标题的左对齐和日期右对齐,在图的主面板中,这是“pre-ggplot”R量子包的标准(参见例如quantmod)。

我的问题是如何使用ggplot2,在情节的titlesubtitle中做到这一点?

编辑。澄清了我在文本理由方面的要求。

r ggplot2
1个回答
1
投票

你可以手动cowplot::draw_text

library(ggplot2)
library(cowplot)
p <- ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    labs(title = "")

ggdraw(p) +
    draw_text("Miles/gallon vs. weight (in 1000 lbs)", x = 0.01, y = 0.99, hjust = 0, vjust = 1) + 
    draw_text("Dataset: mtcars", x = 0.99, y = 0.99, hjust = 1, vjust = 1)

enter image description here

labs(title = "")是为标题创造空白空间所必需的。

© www.soinside.com 2019 - 2024. All rights reserved.