gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:57:42 24 4
gpt4 key购买 nike

我在尝试着

  • 有换行符(自动或强制)
  • 对齐文本(左对齐或左右对齐)
  • 有希腊字母和百分号

  • 在 gglot 图例标签内。

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

    我可以通过插入 \n 来获得换行符进入标签,但这似乎不适用于希腊字母,不适用于图例标签。或者我可以通过组合 mtext() 在基本图中使用换行符和希腊字母和 bquote()将一段文本插入到图中,( 编辑 )但正如 Gregor 在评论部分指出的那样,这不适用于 ggplot .下面我结合一个 listpaste0()将文本传递给图例标签:问题是我找不到插入希腊字母的方法,例如 Gamma 。

    感谢您的建议。

    这是我尝试的 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

    以下是一些我无法发挥作用的有用建议,例如 mtext()的组合和 bquote()在1和2中提到:
  • Expression and new line in plot labels
  • Line break in expression()?
  • Wrap horizontal legend across multiple rows
  • using expression(paste( to insert math notation into a ggplot legend
  • ggplot2 two-line label with expression
  • How to annotate() ggplot with latex
  • Greek letters in ggplot annotate
  • 最佳答案

    Baptiste 的建议是正确的: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 没有。可以将字母 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
    为方便起见,我在下面复制了一些常用的希腊字母的 unicode。可以找到更多的 unicode,例如 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

    关于r - ggplot 图例标签内的希腊字母、符号和换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27690729/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com