在面板外添加ggplot注释?还是两个头衔? [英] Add ggplot annotation outside the panel? Or two titles?

查看:372
本文介绍了在面板外添加ggplot注释?还是两个头衔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ggplot2 动画包制作动画剧情。动画的每个框架都包含一个带有两位信息的地图,我想用它作为标题/标签。

在我看来,有两种方法可以做到这一点。


  1. 调整绘图边距,使其没有标题边距,然后使用 geom_text 在图上添加标题作为文本注释。

  2. 添加第二个标题。 (我尝试用空格填充单个标题以模拟两个标题,但由于字距缩小导致重复绘制,因此分解)。

第一种方法并不理想,因为我需要为每个框架使用相同的绘图限制,并从经纬度/长度测量的地图范围中获取这些限制。确定标签所需的额外高度会导致单位的丑陋混杂。因此,添加第二个标题在语义上似乎更符合我的要求,但在玩弄 annotation_custom 后,我似乎无法弄清楚。



下面是我之后的模型,展示了两个标题,每个都有独立的对齐,以确保它们保持在重复的帧中。我打开了情节边界来帮助观察位置。在最后的情节中,这将被删除。




感谢您的帮助!



编辑

strong>



感谢Baptiste提供的签署信息。我必须使用稍微不同的参数来(a)获得定位权并(b)为两个标题使用相同的字体。这里有一个例子:

  require(gtable)
require(ggplot2)

##创建基本绘图
df < - data.frame(x = 1:10,y = 1:10)
gg < - ggplot(df,aes(x,y))+ geom_point( )+ labs(title =Left)+
theme(plot.title = element_text(hjust = 0))

##从原始图获取标题样式
g < - ggplotGrob(gg)
title_style< - g $ grobs [[8]] $ gp

##添加第二个标题并绘制
g2< - gtable_add_grob (g,textGrob(Right,x = 1,hjust = 1,gp = title_style),
t = 2,l = 4,b = 2,r = 4,name =right-title)
grid.draw(g2)

结果如下:

解决方案

试试这个

  p = qplot(1,1)
g = ggplotGrob(p)

require(gtable)
g = gtable_add_grob(g,grobTree(textGrob(left,x = 0,hjust = 0),
textGrob(right,x = 1,hjust = 1)),
t = 1,l = 4)

grid.draw(g)


I'm making an animated plot using the ggplot2 and animation packages. Each frame of the animation consists of a map with two bits of information that I want to use as titles/labels.

It seems to me there are two ways of doing this.

  1. Adjust the plot margins so there is no title margin and then use geom_text to add the "titles" as text annotations on the plot.
  2. Add a second title. (I've tried padding a single title with spaces to simulate two titles but this breaks down with repeated plots because of kerning.)

The first approach isn't ideal as I need to use the same plot limits for each frame and I get these from the map extents measured in lat/long. Determining the extra height needed for the labels results in an ugly mish-mash of units. Adding a second title therefore seems semantically more sensible to me but after playing around with annotation_custom, I can't seem to figure it out.

Here's a mock-up of what I'm after, showing the two "titles" and each with separate alignments to ensure they stay in place in repeated frames. I've turned on the plot border to help visualize the position. In the final plot, this would be removed.

Thanks for your help!

Edit

Thanks Baptiste for the sign-posting. I had to use slightly different arguments to (a) get the positioning right and (b) use the same font for both titles. Here's an example:

require(gtable)
require(ggplot2)

## Create the basic plot
df <- data.frame(x=1:10, y=1:10)
gg <- ggplot(df, aes(x,y)) + geom_point() + labs(title="Left") +
    theme(plot.title=element_text(hjust=0))

## Get the title style from the original plot
g <- ggplotGrob(gg)
title_style <- g$grobs[[8]]$gp

## Add the second title and plot
g2 <- gtable_add_grob(g, textGrob("Right", x=1, hjust=1, gp=title_style),
                      t=2, l=4, b=2, r=4, name="right-title")
grid.draw(g2)

Here's the result:

解决方案

try this

p = qplot(1,1) 
g = ggplotGrob(p)

require(gtable)
g = gtable_add_grob(g, grobTree(textGrob("left", x=0, hjust=0), 
                                textGrob("right", x=1, hjust=1)), 
                    t=1, l=4)

grid.draw(g)

这篇关于在面板外添加ggplot注释?还是两个头衔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆