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

工作中做图时时常要用到为画出的图添加文字说明,无意中看到有个外国的高级投资策略师写的博客提出了一种方法,所以分享给大家,尊重原创,博客原文: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)

最后生成的图片
这里写图片描述

  • 9
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值