【问题标题】:How can I replace numbers on an axis with symbols in ggplot?如何用ggplot中的符号替换轴上的数字?
【发布时间】:2021-08-31 10:51:25
【问题描述】:

我正在制作散点图,并想用符号替换 y 轴的数字。具体来说,我想将 20 替换为减号,将 0 替换为加号。

我看过this response,但我创建 y 轴的方式有点不同。

谢谢。

这是我的绘图代码:

library(tidyverse)

ggplot(exampledf, aes(x = male_diff, y = index, color = as.factor(expt_in_ms))) + 
  geom_point(aes(alpha = 0.5, size = 3)) +
  geom_point(aes(x = female_diff),alpha = 0.8, shape =24, size = 5) +
  scale_y_continuous(breaks = 1:nrow(exampledf), labels = exampledf$nloci) +
  xlab("difference in proportion") +
  ylab("correlation") +
  geom_vline(
    xintercept = 0,
    color = 'black',
    linetype = 'dashed',
    alpha = .5
  ) +
  theme_minimal() + theme(text = element_text(size = 12)) + guides(alpha=FALSE) + guides(size=FALSE) +
  guides(shape = FALSE) + guides(color = FALSE)

这是绘图代码中名为exampledf的数据集:

structure(list(male_diff = c(0.4668, -0.03299, 0.702, -0.11544, 
0.689, 0.511, -0.0725, -0.12844, -0.0827, 0.6515, -0.01077, 0.006, 
0.0041, -0.00856, 0.4181, -0.02765), female_diff = c(-0.459, 
0.022, -0.155, 0.00800000000000001, -0.156, -0.326, -0.0224, 
0.00700000000000001, 0.0399999999999999, 0.2182, -0.08458, 0.8844, 
-0.8459, 0.122, -0.506, 0.03), expt_in_ms = c(1, 1, 2, 2, 3, 
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4), nloci = c(0, 20, 0, 20, 0, 
0, 0, 20, 20, 20, 0, 20, 0, 20, 0, 20), index = 1:16), row.names = c(NA, 
-16L), class = c("tbl_df", "tbl", "data.frame"))

【问题讨论】:

    标签: r ggplot2 plot unicode


    【解决方案1】:

    您可以在 labels 参数中使用嵌套的 gsub()。在这里,\u2212 是 unicode 减号,但您也可以使用连字符。正则表达式位^ 表示“开始于”,而$ 表示“结束于”,所以^0$ 应该匹配精确的0。

    library(tidyverse)
    
    exampledf <- structure(
      list(male_diff = c(0.4668, -0.03299, 0.702, -0.11544, 0.689, 0.511, -0.0725, 
                         -0.12844, -0.0827, 0.6515, -0.01077, 0.006, 0.0041, 
                         -0.00856, 0.4181, -0.02765), 
           female_diff = c(-0.459, 0.022, -0.155, 0.00800000000000001, -0.156, 
                           -0.326, -0.0224, 0.00700000000000001, 0.0399999999999999,
                           0.2182, -0.08458, 0.8844, -0.8459, 0.122, -0.506, 0.03), 
           expt_in_ms = c(1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4), 
           nloci = c(0, 20, 0, 20, 0, 0, 0, 20, 20, 20, 0, 20, 0, 20, 0, 20), 
           index = 1:16), 
      row.names = c(NA, -16L), class = c("tbl_df", "tbl", "data.frame")
    )
    
    ggplot(exampledf, aes(x = male_diff, y = index, color = as.factor(expt_in_ms))) + 
      geom_point(aes(alpha = 0.5, size = 3)) +
      geom_point(aes(x = female_diff),alpha = 0.8, shape =24, size = 5) +
      scale_y_continuous(
        breaks = 1:nrow(exampledf), 
        labels = gsub("^0$", "+", gsub("^20$", "\u2212", exampledf$nloci))
      ) +
      xlab("difference in proportion") +
      ylab("correlation") +
      geom_vline(
        xintercept = 0,
        color = 'black',
        linetype = 'dashed',
        alpha = .5
      ) +
      theme_minimal() + 
      theme(text = element_text(size = 12)) + 
      guides(alpha="none", size = "none", shape = "none", color = "none")
    

    reprex package (v1.0.0) 于 2021 年 8 月 31 日创建

    【讨论】:

    • labels = factor(exampledf$nloci, labels=c("-", "+")) 足够了......也许不是一个很好的符号
    • 谢谢,我刚刚尝试使用符号 (symbols &lt;- c("+","\u2013","+","\u2013","+", "+", "+", "\u2013", "\u2013", "\u2013", "+", "\u2013", "+", "\u2013", "+", "\u2013")) 创建一个矢量,但您的回复更安全。
    • @user20650 标签的顺序错误,但在这种情况下,您也可以使用它,假设只有两个数字是 0 和 20。
    • 是的。但是在查看U2212 时排版要好得多
    • \u2212 旨在与+ 符号结合使用。两个符号中的水平条纹具有相同的长度。不过,我不是排版师,可能任何读者都会将连字符、en- 和 em-dashes 解释为减号。
    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2021-11-15
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2012-07-21
    • 1970-01-01
    相关资源
    最近更新 更多