R语言中plot函数的双坐标输出

2022-5-16 18:48| 发布者: Hocassian| 查看: 85| 评论: 0|原作者: 樱花庄的白猫

摘要:
C:\Users\Administrator\Downloads\2019-10-14-0-25-44-134316864589699-樱花庄的白猫 ねこ・しろ・ましろ-采集的数据-后羿采集器.html

标题

R语言中plot函数的双坐标输出

标题链接

https://2heng.xin/2017/05/25/plot/

post-date

发布于 2017-05-25

post-meta

2,695 热度

评价数

NOTHING

分类

野生技术协会

正文

期末写货币银行学的论文,要研究利率对房价的影响,需要作一堆图来对比国内外情况,选择了用R绘图(Excel明明半分钟就可以了,我用R写了一个小时TAT),效果如下:

Graph

代码如下:

FUN <- function(){
# 绘制房屋价格指数
plot(x = JP$Year, y = JP$Price,
xlim = c(1970, 2015), ylim = c(20, 120),
pch=16, axes=FALSE, xlab="", ylab="", col="blue", type="b",
main = "House Price Index & Interest Rate in Japan from 1970 to 2015")
# 左侧坐标轴
axis(2, ylim = c(20, 120), col="black", las=1)
# 图示标签
text(1975, 28, labels = "House Price Index", col = "blue")
# 左侧坐标轴标签,注意line参数调整位置
mtext("House Price Index (2010=100)", side=2, line=2.5)
# 允许在同一张图上画第二条折现
par(new=TRUE)
plot(x = JP$Year, y = JP$Interest,
xlim = c(1970, 2015), ylim = c(0, 10),
pch=16, axes=FALSE, xlab="", ylab="", col="red", type="b",
main = "House Price Index & Interest Rate in Japan from 1970 to 2015")
axis(4, at = c(0, 2, 4, 6, 8, 10), col="black", las=1)
mtext("Interest Rate %)", side=4, line=1.3)
text(1974, 7, labels = "Interest Rate", col = "red")
# x轴标签
axis(1, at = c(1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015), col="black", las=2)
}
# 储存为svg(我这么认真画的图,导出为png品质那么低,强迫症不满意:P )
svg(filename="test.svg",
width=12,
height=8,
pointsize=12)
FUN()
dev.off()

事实上这类简单图像用 STATA 画的话会简单且美观很多:
Graph.png

sysuse auto, clear
scatter mpg weight || scatter price weight, yaxis(2)

Q.E.D.

作者

Mashiro


路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部