ggplot图例标签内的希腊字母,符号和换行符

时间:2014-12-29 14:28:40

标签: r ggplot2

我正在尝试

  1. 有换行符(自动或强制)
  2. 证明文本的合理性(左侧或左侧和右侧)
  3. 有希腊字母和百分号
  4. 在gglot图例标签内。

    我已经尝试了几种方法,但我似乎无法将我读到的所有技巧结合起来。

    我可以通过在标签中插入\n来获取换行符,但这似乎不适用于希腊字母,而不适用于图例标签。或者我可以通过组合mtext()bquote()将一段文字插入图中,在基础图中包含换行符和希腊字母,(编辑)但正如Gregor在评论部分,这不适用于ggplot。下面我将listpaste0()合并,将文字传递给图例标签:问题是我无法找到插入希腊字母的方法,例如:伽马。

    感谢建议。

    以下是我的一次尝试的MWE(编辑:改进的MWE):

    label1.line1 <- "Not much to say about this one"
    label2.line1 <- "blabla blabla, blabla blabla, blee blee blee (bling, bling, bling),"
    label2.line2 <- paste0("(", "1900-2014: g = 1.50%, gamma = 2.75%, and r = 2.30%",")")
    label3.line1 <- "blabla blabla, blabla blabla, blee blee blee (bling, bling, bling),"
    label3.line2 <- paste0("(", "1900-2014: g = 2.50%, gamma = 1.75%, and r = 2.30%",")")
    labels_fixed <- list(
        label1.line1, 
        paste0(label2.line1, "\n", label2.line2),
        paste0(label3.line1, "\n", label3.line2)
    )
    library(ggplot2)
    library(scales)
    library(grid)  # provides unit() function used to tweak spacing inside legend
    
    ggplot(data = mtcars, aes(x = mpg, y = wt, group = factor(cyl), colour = factor(cyl), shape = factor(cyl))) + 
    geom_line() + geom_point(size = 3) + theme_bw() + 
    scale_shape_manual(name = "Details", values = c(17, 21, 15), 
        labels = labels_fixed) + 
    scale_colour_manual(name = "Details", values = c("darkred", "darkgreen", "darkblue"), 
        labels = labels_fixed) + 
    theme(legend.key = element_blank(), 
          legend.position = c(.65, .8), 
          legend.background = element_rect(colour = "black"), 
          legend.key.size = unit(2, "lines"), 
          legend.text = element_text(size = 15)) 
    

    一个小问题是图例文字要小得多,因此需要调整图例行间距:我能够调整legend.key.sizelegend.text以获得比默认情况下,这太狭窄了。

    我还尝试了另一个涉及cat(strwrap("long label here"), sep = "\n")的建议,但我无法解决这个问题。我也试过atop,但这会使每一行太小而且实现所需堆叠所需的嵌套是乏味的。

    有没有办法获得希腊字母?

    enter image description here

    以下是一些我无法使用的有用建议,例如: 1和2中提到了mtext()bquote()的组合:

    1. Expression and new line in plot labels
    2. Line break in expression()?
    3. Wrap horizontal legend across multiple rows
    4. using expression(paste( to insert math notation into a ggplot legend
    5. ggplot2 two-line label with expression
    6. How to annotate() ggplot with latex
    7. Greek letters in ggplot annotate

1 个答案:

答案 0 :(得分:27)

巴蒂斯特的建议很明显:unicode走了很长的路。用\u03B3替换上面的每个gamma实例,它将显示为希腊语。惊人!

可以使用cairo设备(ref)进行打印:

library(Cairo)
cairo_pdf(file = "ggplot-greek.pdf", width = 8, height = 5)
## ggplot object created here
dev.off()

也有效:ggsave("greek.pdf", device = cairo_pdf)

以下是我发现有用的unicode符号选择:

"\U016B" = ū
"\U016A" = Ū
"\U00FB" = û
"\U00DB" = Û
"\U0233" = ȳ
"\U0232" = Ȳ
"\U0177" = ŷ
"\U0176" = Ŷ
"\U0113" = ē
"\U0112" = Ē
"\u00EA" = ê
"\U00CA" = Ê
"\U003BC\U2080" = μ₀
"\U003BC\U2081" = μ₁
"R\U00B2" = R²
"\u221E" = ∞
"\u2248" = ≈
"\U2260" = ≠ 
"\u03C3/\u221An" = σ/√n

可悲的是,并非所有特殊字符都具有等效的unicode。例如,有一个用于y-hat的unicode,但没有用于x-hat的unicode。可以将字母x与unicode栏组合在一起,如"x\u0305" = x̅"p\u0302" = p̂,但这似乎打印得不是很好。

我也难以将希腊unicode打印为轴标签,因此在这些情况下,我使用选项annotate() coord_cartesian(clip = "off")来确保打印在轴下方的标签不会被剪裁(“ on是“默认剪裁行为”。

有时你可以直接使用这个角色,例如:

myplot + labs(title  = "±")

当unicode失败时,您可以尝试expression(),例如

title = expression(paste("z = (", bar(x), "-\u03BC)/(\u03C3/\u221An)", sep = ""))  
library(ggplot2)
ggplot() + labs(title = title) + theme_bw() 

title

为方便起见,我在下面复制一些常用的希腊字母'unicodes。可以找到更多的unicodes,例如http://en.wikipedia.org/wiki/List_of_Unicode_characters

\u0391  Α   Greek Capital Letter Alpha
\u0392  Β   Greek Capital Letter Beta
\u0393  Γ   Greek Capital Letter Gamma
\u0394  Δ   Greek Capital Letter Delta
\u0395  Ε   Greek Capital Letter Epsilon
\u0396  Ζ   Greek Capital Letter Zeta
\u0397  Η   Greek Capital Letter Eta
\u0398  Θ   Greek Capital Letter Theta
\u0399  Ι   Greek Capital Letter Iota
\u039A  Κ   Greek Capital Letter Kappa
\u039B  Λ   Greek Capital Letter Lambda
\u039C  Μ   Greek Capital Letter Mu
\u039D  Ν   Greek Capital Letter Nu
\u039E  Ξ   Greek Capital Letter Xi
\u039F  Ο   Greek Capital Letter Omicron
\u03A0  Π   Greek Capital Letter Pi
\u03A1  Ρ   Greek Capital Letter Rho
\u03A3  Σ   Greek Capital Letter Sigma
\u03A4  Τ   Greek Capital Letter Tau
\u03A5  Υ   Greek Capital Letter Upsilon
\u03A6  Φ   Greek Capital Letter Phi
\u03A7  Χ   Greek Capital Letter Chi
\u03A8  Ψ   Greek Capital Letter Psi
\u03A9  Ω   Greek Capital Letter Omega
\u03B1  α   Greek Small Letter alpha
\u03B2  β   Greek Small Letter beta
\u03B3  γ   Greek Small Letter gamma
\u03B4  δ   Greek Small Letter delta
\u03B5  ε   Greek Small Letter epsilon
\u03B6  ζ   Greek Small Letter zeta
\u03B7  η   Greek Small Letter eta
\u03B8  θ   Greek Small Letter theta
\u03B9  ι   Greek Small Letter iota
\u03BA  κ   Greek Small Letter kappa
\u03BB  λ   Greek Small Letter lambda
\u03BC  μ   Greek Small Letter mu
\u03BD  ν   Greek Small Letter nu
\u03BE  ξ   Greek Small Letter xi
\u03BF  ο   Greek Small Letter omicron
\u03C0  π   Greek Small Letter pi
\u03C1  ρ   Greek Small Letter rho
\u03C2  ς   Greek Small Letter final sigma
\u03C3  σ   Greek Small Letter sigma
\u03C4  τ   Greek Small Letter tau
\u03C5  υ   Greek Small Letter upsilon
\u03C6  φ   Greek Small Letter phi
\u03C7  χ   Greek Small Letter chi
\u03C8  ψ   Greek Small Letter psi
\u03C9  ω   Greek Small Letter omega

有关使用unicode的字体的信息:https://en.wikipedia.org/wiki/List_of_typefaces#Unicode_fonts

某些货币符号:

# http://www.fileformat.info/info/unicode/category/Sc/list.htm
Character   Name    Browser     Image
\u0024  DOLLAR SIGN     $
\u00A2  CENT SIGN   ¢
\u00A3  POUND SIGN  £
\u00A4  CURRENCY SIGN   ¤
\u00A5  YEN SIGN    ¥
\u058F  ARMENIAN DRAM SIGN
\u060B  AFGHANI SIGN    ؋
\u09F2  BENGALI RUPEE MARK  ৲
\u09F3  BENGALI RUPEE SIGN  ৳
\u09FB  BENGALI GANDA MARK
\u0AF1  GUJARATI RUPEE SIGN     ૱
\u0BF9  TAMIL RUPEE SIGN    ௹
\u0E3F  THAI CURRENCY SYMBOL BAHT   ฿
\u17DB  KHMER CURRENCY SYMBOL RIEL  ៛
\u20A0  EURO-CURRENCY SIGN  ₠
\u20A1  COLON SIGN  ₡
\u20A2  CRUZEIRO SIGN   ₢
\u20A3  FRENCH FRANC SIGN   ₣
\u20A4  LIRA SIGN   ₤
\u20A5  MILL SIGN   ₥
\u20A6  NAIRA SIGN  ₦
\u20A7  PESETA SIGN     ₧
\u20A8  RUPEE SIGN  ₨
\u20A9  WON SIGN    ₩
\u20AA  NEW SHEQEL SIGN     ₪
\u20AB  DONG SIGN   ₫
\u20AC  EURO SIGN   €
\u20AD  KIP SIGN    ₭
\u20AE  TUGRIK SIGN     ₮
\u20AF  DRACHMA SIGN    ₯
\u20B0  GERMAN PENNY SIGN   ₰
\u20B1  PESO SIGN   ₱
\u20B2  GUARANI SIGN    ₲
\u20B3  AUSTRAL SIGN    ₳
\u20B4  HRYVNIA SIGN    ₴
\u20B5  CEDI SIGN   ₵
\u20B6  LIVRE TOURNOIS SIGN     ₶
\u20B7  SPESMILO SIGN   ₷
\u20B8  TENGE SIGN  ₸
\u20B9  INDIAN RUPEE SIGN   ₹
\u20BA  TURKISH LIRA SIGN   ₺
\u20BB  NORDIC MARK SIGN    ₻
\u20BC  MANAT SIGN  ₼
\u20BD  RUBLE SIGN  ₽
\uA838  NORTH INDIC RUPEE MARK
\uFDFC  RIAL SIGN   ﷼
\uFE69  SMALL DOLLAR SIGN   ﹩
\uFF04  FULLWIDTH DOLLAR SIGN   $
\uFFE0  FULLWIDTH CENT SIGN     ¢
\uFFE1  FULLWIDTH POUND SIGN    £
\uFFE5  FULLWIDTH YEN SIGN  ¥
\uFFE6  FULLWIDTH WON SIGN