我正在尝试使用ggplot2
创建一些标签/贴纸。我正在从源中提取标签的文本,并键入情节。我可以这样做:
labtxt1 <- c("This text should be bold", "This text should also be bold", "Another text to be bold")
labtxt1 <- strwrap(labtxt1, width = 20)
labtxt2 <- c("This text should not be bold", "This text should also not be bold", "Another text to be plain")
labtxt2 <- strwrap(labtxt2, width = 20)
library(ggplot2)
library(Hmisc)
labtxt <- c(labtxt1, labtxt2)
labtxt <- paste(labtxt, collapse = "\n", sep = "")
labtxt <- escapeRegex(labtxt)
ggplot(x=1, y = 1) +
annotate("text", x = 0.5, y = 0.5,
label = labtxt,
colour = "red", parse = F, lineheight = 0.8)
现在如何使文本在labtxt1
中显示为粗体,以及labtxt2
中的文本作为简单的维护理由和linewidth
我试过expression
和\n
,它混淆了理由,和atop
,但是它扰乱了线宽。
如果我尝试单独注释,那么定位是困难的,因为labtxt1
和labtxt2
的长度因不同的标签/贴纸而不同。
如何获得预期的结果?
发布于 2018-04-16 02:21:14
你可以把文字包装在一个看不见的表格里,
library(ggplot2)
library(gridExtra)
label <- "plain text\nbold('bold line')\nitalic('italic line')\nplain again"
disect <- strsplit(label, "\\n")[[1]]
mytheme <- ttheme_minimal(core = list(fg_params = list(parse=TRUE, col="red",
hjust=0, x=0.1)))
tg <- tableGrob(as.matrix(disect), theme=mytheme)
ggplot(x=1, y = 1) +
annotation_custom(tg)
https://stackoverflow.com/questions/49854480
复制