简体   繁体   English

ggplot2 标题和副标题之间的线

[英]ggplot2 line between title and subtitle

Is there a way to include a line, like a separator, between the title and the subtitle with ggPlot?有没有办法在 ggPlot 的标题和副标题之间包含一条线,如分隔符? I know how to color the background of the title (see this Post)我知道如何给标题的背景上色(见这篇文章)

One possible solution is to manually trace a segment using geom_segment while setting clip = "off" on coord_cartesian :一种可能的解决方案是在coord_cartesian上设置clip = "off"时使用geom_segment手动跟踪段:

df <- data.frame(x = 1:10,
                 y = 2:11)

ggplot(df, aes(x = x, y = y))+
  geom_point()+
  labs(title = "Main Title", subtitle = "Subtitle")+
  coord_cartesian(clip ="off")+
  geom_segment(x =0.5, xend = 2.5, y = 12.1, yend = 12.1)

在此处输入图像描述

You will have to adapt the position based on data you are going to plot but it is one possible solution您将不得不根据您要转到 plot 的数据来调整 position,但这是一种可能的解决方案

Another option simply to underline your Main title另一个选项只是在您的主标题下划线

library(ggplot2)
df <- data.frame(x = 1:10,
                 y = 2:11)
ggplot(df, aes(x = x, y = y)) + 
  geom_point() +
  labs(title = ~ underline("Main Title"), subtitle = "Subtitle")

related thread: How to underline text in a plot title or label?相关主题: 如何在 plot 标题或 label 中为文本添加下划线? (ggplot2) (ggplot2)

You could also play around in order to achieve a solution as suggested by user Stephane Laurent in their comment, pasting some empty spaces after a line break您也可以按照用户 Stephane Laurent 在评论中的建议来实现解决方案,在换行符后粘贴一些空格

Created on 2020-04-22 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 4 月 22 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM