【问题标题】:Use superscripts in text with paste (for a vector of several values) in R在 R 中使用带有粘贴(用于多个值的向量)的文本中的上标
【发布时间】:2018-08-18 12:15:21
【问题描述】:

我想要在我的图例中带有上标的标签。此外,我想用粘贴功能构建标签。我已经想出了如何使用paste 使用我的值向量的图例。我还使用expression 来获取上标。但我不能同时使用它们。

这是我的尝试:

带有图例的绘图成功地根据值向量绘制了三个图例项,但图例中没有上标。

#set-up
size=c(50, 100, 150)
paste(size, "km^2", sep=" ")
#output: paste function works
# "50 km^2"  "100 km^2" "150 km^2"

#plot sample graph
plot(x=c(1:10)*100, y=c(1:10)*10, col="red")
points(x=c(8:17)*50, y=c(1:10)*7, col="green")
points(x=c(8:17)*50, y=c(1:10)*12, col="blue")
#legend
legend(x = 'topleft', 
       legend = paste(size, "km^2", sep=" "),
       col = c("red", "green", "blue"), pch=19,bty = 'n', xjust = 1, cex=0.8)

在这里我可以在图表上使用上标打印但不粘贴。

#print with superscript
mtext(line=-4,adj=0,expression('km'^'2'*' size '))

失败的图表

【问题讨论】:

  • 如果您只需要正方形,为什么不使用 Unicode 字符作为正方形 km²
  • @G5W 你能扩展一下吗?我不熟悉 Unicode 字符,我的谷歌搜索没有让我得到任何有用的东西
  • 您可以从我的评论km² 中剪切和粘贴字符,或者只是通过谷歌找到它们。例如,维基百科在Unicode subscripts and superscripts 上有一个页面,只需剪切并粘贴您需要的页面即可。 x² x³ x⁴ x⁵ x⁶ x⁷ x⁸ x⁹ 将其粘贴到您的图例语句中。
  • legend(x = 'topleft', legend = paste(size, 'km² size ', sep=" "), col = c("red", "green", "blue"), pch=19, bty = 'n', xjust = 1, cex=0.8)

标签: r expression paste subscript superscript


【解决方案1】:

您可以使用pasteparse 创建表达式字符串向量。

plot(x=c(1:10)*100, y=c(1:10)*10, col="red")
points(x=c(8:17)*50, y=c(1:10)*7, col="green")
points(x=c(8:17)*50, y=c(1:10)*12, col="blue")

#legend
legend(x = 'topleft', 
       legend = parse(text=paste(size, "*km^2~size")),
       col = c("red", "green", "blue"), pch=19, bty = 'n', xjust = 1, cex=0.8)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    相关资源
    最近更新 更多