【问题标题】:R Create multiple graphs with different title and axis names with subscripts and superscriptR使用下标和上标创建具有不同标题和轴名称的多个图形
【发布时间】:2019-05-30 10:32:59
【问题描述】:

我花了几个小时搜索并试图找到一种使用表达式或 bquote 的方法来为我制作的每个图表创建一个新标题。我遇到的问题是每个图表的标题名称都会发生变化,并且每个名称中有不同数量的下标和上标。如何格式化我的“main=”语句以使用我想要的各种下标和上标更改为新的标题名称?

# This is the format I want to show on the graphs
# Title Graph1 = "Total pH"  (no super or subscripts)
# Title Graph2 = "Total NO3-"  where "3" subscript and "-" superscript
# Title Graph3 = "Total H2PO3-" where "2" subscript, "3" subscript, and "-" superscript
# Title Graph4 = "Total K+" where "+" is superscript

n <- c("pH", "NO3-", "H2PO3-", "K+")

for (j in n) {
    dev.new(width=10, height = 10)
    plot (x=2,y=3, main=j)
}

【问题讨论】:

  • 我应该补充一下,我希望为轴标签以及主要标签执行此操作,这就是我添加标签轴标签的原因。我假设如果有解决方案,相同的过程将适用于两个绘图功能。

标签: r plot axis-labels


【解决方案1】:

一种方法是使用expressionplotmath 符号来创建所需表达式的向量,然后对它们进行排序。

n<-expression(pH, NO['3']^'-', H[2]~PO[3]^'-', K^'+')
for (j in n) {
  dev.new(width=10, height = 10)
  plot (x=2,y=3, main=j )
}

有关符号的语法,请参阅?plotmath

【讨论】:

  • 谢谢。我尝试了所有我能想到的组合,除了使用“
【解决方案2】:

我在这个问题上又添了一个麻烦。我还想在标题名称中添加不同的文本以及添加下标。我终于让它与“bquote”命令一起工作。只需添加此代码示例,以防其他人遇到此类问题并正在寻找解决方案。

# This is the format I want to show on the graphs
# Title Graph1 = "Houston - pH"  (no super or subscripts)
# Title Graph2 = "Los Angeles - (NO3-)"  where "3" subscript and "-" superscript
# Title Graph3 = "New York - (H2PO3-)" where "2" subscript, "3" subscript, and "-" superscript
# Title Graph4 = "Omahaa - (K+)" where "+" is superscript

SampleName <- c("Houston", "Los Angeles", "New York", "Omaha")

for (j in 1:4) {
    if (j==1) {
        n <-as.expression(bquote(.(SampleName[j])~'(pH)'))
    } else if (j==2) {
        n <-as.expression(bquote(.(SampleName[j])~'(NO'['3']^'-'*')'))
    } else if (j==3) {
        n <-as.expression(bquote(.(SampleName[j])~'(H'['2']*'PO'['3']^'-'*')'))
    } else {
        n <-as.expression(bquote(.(SampleName[j])~'(K'^'+'*')'))
    }
  dev.new(width=10, height = 10)
  plot (x=2,y=3, main=n )
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 2020-09-22
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2021-07-29
    相关资源
    最近更新 更多