我喜欢在我的图表中添加以下标题:
注:美国、英国和荷兰的市场集中度平均值分别为 1920、1388 和 1244
其中“注:”需要斜体,“荷兰分别为1920、1388和1244”应换行。
使用该paste功能,我可以将一部分斜体化。但是使用\ninside paste,可以将所有内容混合在一起,正如您在此处看到的那样(这是经过编辑的图像,使用以下 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)))
Run Code Online (Sandbox Code Playgroud)
小智 7
基于这个答案,
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()))
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5044 次 |
| 最近记录: |