在tableGrob中添加多行脚注,同时在R中使用gridextra

时间:2015-11-10 04:57:53

标签: r ggplot2 gridextra

我最近开始使用tableGrob和gridextra来组合多个图表和表格。我希望mt tableGrob有一个脚注和标题。

以下链接很好地回答: Adding text to a grid.table plot

但是在上面的代码中,脚注会被截断太长时间。有人可以建议一个替代方案,以便脚注一旦到达桌子末端就自动包裹到下一行吗?如果它可以包裹在单词的中间也可以。

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect frame = [[UIScreen mainScreen] bounds];

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)];
    lbl1.backgroundColor = [UIColor redColor];
    lbl1.textAlignment = NSTextAlignmentCenter;
    lbl1.text = @"Dashboard";
    [self.navigationController.navigationBar addSubview:lbl1];

    UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)];
    lbl2.backgroundColor = [UIColor redColor];
    lbl2.textAlignment = NSTextAlignmentCenter;
    lbl2.text = @"Profile";
    [self.navigationController.navigationBar addSubview:lbl2];
}

Long footnote tableGrob

当我有一个情节和一个网格排列表时,我希望这个工作。请帮忙。

我尝试使用strwrap并将宽度设置为grobWidth,但它对我不起作用。

1 个答案:

答案 0 :(得分:4)

RGraphics书籍/包提供了一种可能的解决方案,

splitString <- function (text, width) {
  strings <- strsplit(text, " ")[[1]]
  newstring <- strings[1]
  linewidth <- stringWidth(newstring)
  gapwidth <- stringWidth(" ")
  availwidth <- convertWidth(width, "in", valueOnly = TRUE)
  for (i in 2:length(strings)) {
    width <- stringWidth(strings[i])
    if (convertWidth(linewidth + gapwidth + width, "in", 
                     valueOnly = TRUE) < availwidth) {
      sep <- " "
      linewidth <- linewidth + gapwidth + width
    }
    else {
      sep <- "\n"
      linewidth <- width
    }
    newstring <- paste(newstring, strings[i], sep = sep)
  }
  newstring
}


tit <- "Title is long too or is it??"
foot <- "footnote is pretty longgg but not unusually longgggggggggkjwd jwkldn"
footnote <- textGrob(splitString(foot, sum(t1$widths)))
title <- textGrob(splitString(tit, sum(t1$widths)))
t1 <- gtable_add_rows(t1, heights = grobHeight(footnote))
t1 <- gtable_add_rows(t1, heights = grobHeight(title), 0)
t1 <- gtable_add_grob(t1, list(title, footnote),
                      t=c(1, nrow(t1)), l=1, r=ncol(t1))

grid.draw(t1)