ggplot格式斜体注释

17

我能在ggplot注释中使用标记吗?

假设我有这张图:

p <- function(i) 8*i
a <- function(i) 1+4*i*(i-1)

library(ggplot2)
library(reshape2)

i <- 1:(8*365/7)
d <- data.frame(i=i,p=p(i),a=sapply(i,a))
d <- melt(d, id.vars='i')
p <- ggplot(d, aes(i, value, linetype=variable)) +
    geom_hline(yintercept=700^2) +
    geom_line() +
    scale_linetype_manual(values=c(2,1)) +
    #geom_point() +
    scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) +
    #scale_y_continuous(breaks=c(0,700^2), labels=c(0,expression(L^2)))
    scale_y_sqrt() +
    #scale_y_log10() +
    annotate('text', 8*365/7, 1e3, label="P(i)=8i", hjust=1, size=3) +
    annotate('text', 8*365/7, 2.5e5, label="A(i)=1+4i(i-1)", hjust=1, size=3)
print(p + theme_classic())

output

我知道我可以使用 fontface=3 并将所有内容放在斜体中。但我不想让数字变成斜体,只想让变量 i 斜体显示。最好不要让 PA 也变成斜体。

有什么建议吗?

4个回答

28

现在这个页面是谷歌搜索“ggplot annotate italic”的最高搜索结果。为了让那些只想给整个注释加斜体的人受益,我写了这篇文章。使用annotate的fontface选项。例如:

现在这个页面是谷歌搜索“ggplot annotate italic”的最高搜索结果。为了让那些只想给整个注释加斜体的人受益,我写了这篇文章。使用annotate的fontface选项。例如:

seq(0,3.14,0.01)
qplot(x, sin(x)) +   # works the same for qplot or ggplot
annotate(geom = 'text', 
         x = 1.5, 
         y = 0.5, 
         hjust = 0.5,
         label = 'Hello, World', 
         fontface = 'italic')

输入图像描述


1
请注意,fontface参数也适用于geom_text - pbnelson

23

使用parse=TRUE并提供一个格式符合?plotmath的字符串。

p <- ggplot(d, aes(i, value, linetype=variable)) +
    geom_hline(yintercept=700^2) +
    geom_line() +
    scale_linetype_manual(values=c(2,1)) +
    scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) +
    scale_y_sqrt() +
    annotate('text', 8*365/7, 1e3,
             label="P(italic(i))==8~italic(i)", parse=TRUE,
             hjust=1, size=3) +
    annotate('text', 8*365/7, 2.5e5,
             label="A(italic(i))==1+4~italic(i)(italic(i)-1)", parse=TRUE, 
             hjust=1, size=3)

输入图像说明


7

最佳答案已经很好了,但在更复杂的场景中,包含换行符时,它对我不起作用,所以我简单地使用 Unicode 斜体字符代替。对于您的示例:

library(Unicode)

italic_i <- u_char_inspect(u_char_from_name("MATHEMATICAL ITALIC SMALL I"))["Char"]
label1 <- paste("P(", italic_i, ")=8", italic_i, sep="")
label2 <- paste("A(", italic_i, ")=1+4", italic_i, "(", italic_i, "-1)", sep="")

i <- 1:(8*365/7)
d <- data.frame(i=i,p=p(i),a=sapply(i,a))
d <- melt(d, id.vars='i')
p <- ggplot(d, aes(i, value, linetype=variable)) +
  geom_hline(yintercept=700^2) +
  geom_line() +
  scale_linetype_manual(values=c(2,1)) +
  #geom_point() +
  scale_x_continuous(breaks=(0:20)*365/7, labels=0:20) +
  #scale_y_continuous(breaks=c(0,700^2), labels=c(0,expression(L^2)))
  scale_y_sqrt() +
  #scale_y_log10() +
  annotate('text', 8*365/7, 1e3, label=label1, hjust=1, size=3) +
  annotate('text', 8*365/7, 2.5e5, label=label2, hjust=1, size=3)
print(p + theme_classic())

斜体注释 ggplot2

编辑:我刚刚注意到使用pdf()保存pdf文件不能正确地呈现unicode字符,但是您可以简单地使用cairo_pdf()代替,这个方法完全可行(请参见:ggplot2 pdf输出中的Unicode字符)。


3

对于简单的文本格式化,请使用ggtext包

如果您不经常使用plotmath,并发现使用简单的markdown/html样式文本格式更容易-请使用最近添加的ggtext包

即使这个例子比OP问题更简单(类似于@pbnelson),在此添加对新版ggtext包的引用

library(ggplot2)
library(ggtext)
#> Warning: package 'ggtext' was built under R version 4.0.3
library(tibble)
#> Warning: package 'tibble' was built under R version 4.0.3
dfx <- tibble(x=seq(0,3.14,0.01))
ggplot(dfx, aes(x, sin(x))) +
  geom_line() + # works the same for qplot or ggplot
    geom_richtext(x = 1.5, 
                  y = 0.5, 
                  label.color = NA,
                  label = "<i><b>Hello</i></b>, World<br>You can even do 
                  Some <span style='color:blue'>blue text **in bold.**</span><br>And *italics text.*"
                  )

2020年12月17日由reprex package (v0.3.0)创建


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接