【问题标题】:ggplot2 line between title and subtitle标题和副标题之间的ggplot2行
【发布时间】:2020-08-05 03:39:46
【问题描述】:

有没有办法在 ggPlot 的标题和副标题之间包含一条线,例如分隔符?我知道如何给标题的背景上色(见this Post)

【问题讨论】:

  • 不是很好,但你可以做到:ggtitle("My title\n_______________________________________________", "My subtitle")

标签: r ggplot2


【解决方案1】:

另一个选项只是在你的主标题下划线

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")

相关话题:How to underline text in a plot title or label? (ggplot2)

您也可以按照用户 Stephane Laurent 在他们的评论中的建议来实现解决方案,在换行符后粘贴一些空格

reprex package (v0.3.0) 于 2020 年 4 月 22 日创建

【讨论】:

  • 感谢您的贡献。不幸的是,我还没有找到一种以动态方式整合这些解决方案的方法。无论如何,在 ggPlot 的未来更新中可能会出现类似的情况
【解决方案2】:

一种可能的解决方案是在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)

您必须根据要绘制的数据调整位置,但这是一种可能的解决方案

【讨论】:

  • 感谢您的评论!对单个地块非常有效。假设没有这样的解决方案可以将此分隔线包含在主题中,我是否正确?
  • 不客气 ;) 对于你的问题,据我所知,我不这么认为(但也许我错了)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 2011-02-18
相关资源
最近更新 更多