【问题标题】:Subscript and Superscripts on characters vectors字符向量上的下标和上标
【发布时间】:2021-03-21 21:21:37
【问题描述】:

我希望我的字符向量上同时有下标和上标,相对于绘图上的 X 和 Y 值。

例如,X 值为上标,Y 值为下标。

下面是它应该是什么样子的示例:

我尝试粘贴上标然后绘制它们,尽管效果不如预期

剧情代码:

ggplot(test, aes(x=Neutron, y = Protons, group = Isotopes)) + geom_point() +geom_line() +geom_label(label = test$Isotopes) + scale_x_continuous(breaks = test$Neutron) + scale_y_continuous(breaks = test$Protons)
> ggplot(test, aes(x=Neutron, y = Protons)) + geom_point() +geom_line() +geom_label(label = test$Isotopes) + scale_x_continuous(breaks = test$Neutron) + scale_y_continuous(breaks = test$Protons)

可重现的代码:

structure(list(Neutron = c(237, 233, 233, 229, 225, 225, 221, 
217, 213, 213, 209, 209, 205), Protons = c(93, 91, 92, 90, 88, 
89, 87, 85, 83, 84, 82, 83, 81), Isotopes = c("Np", "Pa", "U", 
"Th", "Ra", "Ac", "Fr", "At", "Bi", "Po", "Pb", "Bi", "Tl")), class = "data.frame", row.names = c(NA, 
-13L))

【问题讨论】:

    标签: r


    【解决方案1】:

    我们可以使用sprintfpaste 来创建标签,然后在aes 中指定label 并在parse 中指定parse geom_label

    library(ggplot2)
    lbl1 <- with(test, sprintf('%s[%d]^%d', Isotopes, Protons, Neutron))
    ggplot(test, aes(x=Neutron, y = Protons, label = lbl1)) +
            geom_point() +
            geom_line() +
            geom_label(parse= TRUE) +
            scale_x_continuous(breaks = test$Neutron) + 
             scale_y_continuous(breaks = test$Protons)
    

    -输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      相关资源
      最近更新 更多