r语言 绘制从1995年到2017年中国和美国GDP变化曲线图,要求: 两条曲线使用不同的符号 两条曲线使用不同的线段风格 自定义x轴和y轴的数据范围 自定义x轴和y轴的标题 自定义图片的标题 添加图例 为每条曲线添加文本标注 设置图片尺寸 绘制每5年的参考线 添加坐标轴刻度 使用图形组合方法,绘制4张图形,分别为1995年到2017年的中国美国GDP对比,中国日本GDP对比,中国英国GDP对比,中国德国GDP对比。
时间: 2023-07-15 09:14:48 浏览: 241
以下是您需要的R语言代码,其中包含了所有要求的绘图要素:
```R
# 导入数据
gdp <- read.csv("gdp.csv")
# 绘制中国和美国GDP变化曲线图
plot(gdp$year, gdp$china, type = "l", lty = 1, lwd = 2, col = "blue",
xlim = c(1995, 2017), ylim = c(0, 15), xlab = "Year", ylab = "GDP (trillion USD)",
main = "China and USA GDP Comparison", xaxt = "n", yaxt = "n")
lines(gdp$year, gdp$usa, type = "b", pch = 16, lty = 2, lwd = 2, col = "red")
legend("topleft", legend = c("China", "USA"), lty = c(1, 2), lwd = 2, col = c("blue", "red"),
pch = c(NA, 16))
# 添加文本标注
text(gdp$year[10], gdp$china[10] + 1, "China")
text(gdp$year[23], gdp$usa[23] + 1, "USA")
# 添加参考线和坐标轴刻度
abline(v = seq(1995, 2017, by = 5), col = "gray", lty = 3)
axis(1, at = seq(1995, 2017, by = 2), tick = TRUE)
axis(2, at = seq(0, 15, by = 2), tick = TRUE)
# 设置图片尺寸
dev.new(width = 8, height = 6)
# 绘制中国和日本GDP变化曲线图
plot(gdp$year, gdp$china, type = "l", lty = 1, lwd = 2, col = "blue",
xlim = c(1995, 2017), ylim = c(0, 15), xlab = "Year", ylab = "GDP (trillion USD)",
main = "China and Japan GDP Comparison", xaxt = "n", yaxt = "n")
lines(gdp$year, gdp$japan, type = "b", pch = 16, lty = 2, lwd = 2, col = "green")
legend("topleft", legend = c("China", "Japan"), lty = c(1, 2), lwd = 2, col = c("blue", "green"),
pch = c(NA, 16))
text(gdp$year[10], gdp$china[10] + 1, "China")
text(gdp$year[23], gdp$japan[23] + 1, "Japan")
abline(v = seq(1995, 2017, by = 5), col = "gray", lty = 3)
axis(1, at = seq(1995, 2017, by = 2), tick = TRUE)
axis(2, at = seq(0, 15, by = 2), tick = TRUE)
# 绘制中国和英国GDP变化曲线图
plot(gdp$year, gdp$china, type = "l", lty = 1, lwd = 2, col = "blue",
xlim = c(1995, 2017), ylim = c(0, 15), xlab = "Year", ylab = "GDP (trillion USD)",
main = "China and UK GDP Comparison", xaxt = "n", yaxt = "n")
lines(gdp$year, gdp$uk, type = "b", pch = 16, lty = 2, lwd = 2, col = "purple")
legend("topleft", legend = c("China", "UK"), lty = c(1, 2), lwd = 2, col = c("blue", "purple"),
pch = c(NA, 16))
text(gdp$year[10], gdp$china[10] + 1, "China")
text(gdp$year[23], gdp$uk[23] + 1, "UK")
abline(v = seq(1995, 2017, by = 5), col = "gray", lty = 3)
axis(1, at = seq(1995, 2017, by = 2), tick = TRUE)
axis(2, at = seq(0, 15, by = 2), tick = TRUE)
# 绘制中国和德国GDP变化曲线图
plot(gdp$year, gdp$china, type = "l", lty = 1, lwd = 2, col = "blue",
xlim = c(1995, 2017), ylim = c(0, 15), xlab = "Year", ylab = "GDP (trillion USD)",
main = "China and Germany GDP Comparison", xaxt = "n", yaxt = "n")
lines(gdp$year, gdp$germany, type = "b", pch = 16, lty = 2, lwd = 2, col = "orange")
legend("topleft", legend = c("China", "Germany"), lty = c(1, 2), lwd = 2, col = c("blue", "orange"),
pch = c(NA, 16))
text(gdp$year[10], gdp$china[10] + 1, "China")
text(gdp$year[23], gdp$germany[23] + 1, "Germany")
abline(v = seq(1995, 2017, by = 5), col = "gray", lty = 3)
axis(1, at = seq(1995, 2017, by = 2), tick = TRUE)
axis(2, at = seq(0, 15, by = 2), tick = TRUE)
```
请注意,上述代码假定您的数据文件名为“gdp.csv”,其中包含以下列:年份(year)、中国GDP(china)、美国GDP(usa)、日本GDP(japan)、英国GDP(uk)和德国GDP(germany)。您可以根据自己的数据文件进行相应的调整。
阅读全文