您的位置:首页 > 其它

【翻译】(R语言)如何为图片添加文字说明

2015-11-10 23:29 591 查看
工作中做图时时常要用到为画出的图添加文字说明,无意中看到有个外国的高级投资策略师写的博客提出了一种方法,所以分享给大家,尊重原创,博客原文:http://datascienceplus.com/adding-text-to-r-plot/

下面介绍了n中为图片添加文字的方法。

# 利用layout函数分割屏幕
layout(matrix(c(1,1,2,2), 2, 2, byrow = T), heights = c(4,1))

# 随意画一张图
plot(rnorm(10),ylab="", xlab="Use the xlab argument", main="Use the title", ty="o", pch=19)

# 添加文字说明
mtext("Use the mtext() function")
temp <- locator(1) # 在图表上,你喜欢的地方点击一下,文字就出来了
text(temp,"Use the text() with or without the locator() function")

# 用下面的Corner_text函数添加文字(自己编写的函数):
Corner_text <- function(text, location="topright"){
legend(location,legend=text, bty ="n", pch=NA)
}
Corner_text(text="Use Corner_text() function")
Corner_text(text="Use the Corner_text() function",location= "bottomright")

# 利用函数title 添加文字说明
title(sub="Add subtitle using the title() function")

# 利用包gplots添加更多文字
library(gplots)

# ?textplot # 查询这个包的信息

# layout.show(2) # 在分割屏幕的中间显示内容(上面的屏幕和下面的屏幕之间)
temptext1 <- "Add here notes. The plot above represents 10 random points drawn from Normal distribution. The plot is generated in order
to visualize all the different options one can use for adding text to a plot.
You can control this text size using the usual \"cex\" argument.
\nYou can increase readability if you skip lines.
\nUse this space when you have complicated charts that require some more details. Add explanations and preemt possible questions, by that
saving readers' time (e.g. \"is it percentage or basis points?\" )."
temptext2 <-  "You can also add more text in the usual way by using again the locator(1)+text() functions"
textplot(temptext1, valign="center", cex=0.8, halign= "left", mar=c(0,0,0,0), col=2)

# mar=c(0,0,0,0) 除去页面空白
temp <- locator(1)
text(temp, temptext2,col=4)


最后生成的图片

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  r语言