【问题标题】:ggplot italicize part of caption AND divide text over two linesggplot斜体部分标题并将文本分成两行
【发布时间】:2017-11-08 18:17:09
【问题描述】:

我喜欢在我的图表中添加以下标题:

注:美国市场集中度平均值,美国 王国和荷兰分别是 1920 年、1388 年和 1244 年

其中“注意:”需要斜体,“荷兰分别为 1920、1388 和 1244”应换行。

使用paste 函数,我可以将一部分斜体。但是在paste 中使用\n 会将所有内容混合在一起,如您在此处看到的那样(这是一张经过编辑的图像,根据下面 Paul 的建议制作):

我尝试了各种其他解决方案,但都没有成功。这是我正在使用的代码:

library(ggplot2)

note = expression(paste(italic("Note: "), "Market concentration averages in the United States, United Kingdom, and the \nNetherlands are, respectively, 1920, 1388, and 1244"))

gg <- ggplot(mtcars, aes(wt, mpg)) + geom_point()+

# Title
labs(caption=note)

gg + theme(plot.caption=element_text(size=7.5, hjust=0, margin=margin(t=15)))

【问题讨论】:

  • 我会节省一些挫败感和 grob-machinations 并使用 magick 对标题进行后期注释。
  • 那将是最佳的@Gregor,但在带有换行符和混合格式的 grobs 中仍然存在对齐错误。从长远来看,magick 注释会容易得多。
  • @Gregor 我明白你的意思。我现在包含了一个最小且可重复的示例。
  • @hrbrmstr 谢谢,如果所有其他解决方案都失败了,我会尝试后注释

标签: r ggplot2 paste


【解决方案1】:

基于this answer

library(grid)
library(ggplot2)


ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_line() + 
  labs(caption= "First~line \n italic('and a second,')~bold('fancy one') \n
       'also,'~integral(f(x)*dx, a, b)~'for good measure'")+
  (theme_grey() %+replace% theme(plot.caption = element_custom()))

element_custom <- function() {
  structure(list(), class = c("element_custom", "element_text"))
}

element_grob.element_custom <- function(element, label="", ...)  {
  disect <- strsplit(label, "\\n")[[1]]
  labels <- lapply(disect, function(x) tryCatch(parse(text=x), 
                                                error = function(e) x))
  hl <-  unit(rep(1, length(labels)), 'strheight', data=labels) + unit(0.1,"line")
  yl <- c(list(unit(0,"line")), 
          lapply(seq_along(labels[-length(labels)]), function(ii) sum(hl[1:ii])))

  cl <- do.call(gList, Map(function(label, ii) 
    textGrob(label, y = unit(1,"npc")-yl[[ii]], hjust=0, x=0, vjust=1), 
    label = labels, ii = seq_along(labels)))

  gTree(children = cl, cl="sl", heights = hl, gp=gpar(col="grey50",fontsize=8))
}

heightDetails.sl <- function(x) sum(x$heights)

【讨论】:

    【解决方案2】:

    这能给你想要的吗?

    note = expression(paste(italic("Note: \n "), 
                            "Market concentration averages in the United States, United Kingdom, and the \nNetherlands are, respectively, 1920, 1388, and 1244"))
    

    (不同之处在于“Note”部分现在还包含换行符)

    【讨论】:

    • 亲爱的保罗,这几乎可以满足我的需求。根据期刊指南,第二行也需要从第一行开始。所以“Note”和“Netherlands”都从 Y 轴开始。我尝试了一些东西,但这也没有奏效。非常欢迎任何建议!
    【解决方案3】:

    这是一种不同的方法:只需在 ggplot2 绘图中创建空白区域(通过添加空白标题),然后导航到 ggplot2 为标题创建的视口并将标题绘制为单独的文本行。

    library(grid)
    
    ggplot(mtcars, aes(wt, mpg)) + geom_point() + 
           labs(caption=expression(atop(" ", " ")))
    
    grid.force()
    downViewport("caption.9-4-9-4")
    ## grid.rect()
    grid.text(expression(paste(italic("Note: "), 
                               "Market concentration averages in the United States, United Kingdom, and the")),
              x=0, y=unit(1, "npc") - unit(1, "line"), just=c("left", "top"),
              gp=gpar(fontsize=7.5))
    grid.text("Netherlands are, respectively, 1920, 1388, and 1244",
              x=0, y=unit(1, "npc") - unit(2, "line"), just=c("left", "top"),
              gp=gpar(fontsize=7.5))
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多